The project uses the Elasticsearch, using JSON format Query method, there are several places in a query statement need to replace, and replace the same value. The first way to convert JSON to string, using the Format function, to find that back to JSON is not successful, colleagues remind you can use the JINJA2 template processing strings,
Second_search = "" "
{
"Query": {
"bool": {
"should": [
{
"bool": {
"should": [
{
"term": {
"{{prod}}_name_en.text": {
"value": "{{keyword}}",
"Boost": Ten
}
}
},
{
"term": {
' {{prod}}_name_zh.text ': {
"value": "{{keyword}}",
"Boost": Ten
}
}
}
]
}
},
{
"bool": {
"should": [
{
"Wildcard": {
' {{prod}}_name_en.text ': {
"value": "{{keyword}}*",
"Boost": 5
}
}
},
{
"Wildcard": {
' {{prod}}_name_zh.text ': {
"value": "{{keyword}}*",
"Boost": 5
}
}
}
]
}
},
{
"Query_string": {
' Fields ': [' {{prod}}_name_en ', ' {{Prod}}_name_zh '],
"Query": "{{keyword}}",
"Boost": 1
}
}
]
}
}
}
"""
SECOND_TPL = Template (Second_search)
Second_str = Second_tpl.render (Keyword=kws, prod= ' other ')
Second_json = Json.loads (SECOND_STR)
It's going to be nice to turn the string into a JSON object after this processing.
The Template in JINJA2 bulk replaces the contents of a JSON string