Searching using query string with variable clauses

Hi everyone.

I'm trying to make a template wich I can use to search through several fields by one word (using query string). But now I need to add a clause which will get a variable in a query (date). How can I combine search with query string with a variable clause?
My template looks like this now:

PUT _scripts/template_id
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{
"match": {
"field_1": "true"
}
},
{
"match": {
"field_2": "true"
}
},
{
"match":{
"field_3":"true"
}
},
{
"bool": {
"should": [
{
"match": {
"field_4": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 4
}
}
},
{
"match": {
"field_5": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 4
}
}
},
{
"match": {
"field_6": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 4
}
}
},
{
"match": {
"field_7": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 1
}
}
}
]
}
}
]
}
},
"from": "{{from}}{{^from}}0{{/from}}",
"size": "{{size}}{{^size}}10{{/size}}"
},
"params": {
"query_string": "Query string"
}
}
}


GET instruments_keyword/_search/template
{
"id": "template_id",
"params": {
"query_string": "apple",
"range": {
"Date": {
"gte": "2023-05-17",
"lte": "2100-01-01"
}
}
"from": 0,
"size": 1280
}
}

So I need to use date range in my query of template and the date must be variable.

Hi @orlenkoda5.

You can add filter with range query, look this:

POST _render/template
{
  "source": {
    "query": {
      "bool": {
        "filter": [
          {
            "range": {
              "Date": {
                "gte": "{{gte}}",
                "lte": "{{lte}}"
              }
            }
          }
        ],
        "must": [
          {
            "match": {
              "field_1": "true"
            }
          },
          {
            "match": {
              "field_2": "true"
            }
          },
          {
            "match": {
              "field_3": "true"
            }
          },
          {
            "bool": {
              "should": [
                {
                  "match": {
                    "field_4": {
                      "query": "{{query_string}}",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_5": {
                      "query": "{{query_string}}",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_6": {
                      "query": "{{query_string}}",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_7": {
                      "query": "{{query_string}}",
                      "fuzziness": "1",
                      "boost": 1
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    },
    "from": "{{from}}{{^from}}0{{/from}}",
    "size": "{{size}}{{^size}}10{{/size}}"
  },
  "params": {
    "query_string": "Query string",
    "gte": "2023-05-17",
    "lte": "2100-01-01"
  }
}

Output:

{
  "template_output": {
    "query": {
      "bool": {
        "filter": [
          {
            "range": {
              "Date": {
                "gte": "2023-05-17",
                "lte": "2100-01-01"
              }
            }
          }
        ],
        "must": [
          {
            "match": {
              "field_1": "true"
            }
          },
          {
            "match": {
              "field_2": "true"
            }
          },
          {
            "match": {
              "field_3": "true"
            }
          },
          {
            "bool": {
              "should": [
                {
                  "match": {
                    "field_4": {
                      "query": "Query string",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_5": {
                      "query": "Query string",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_6": {
                      "query": "Query string",
                      "fuzziness": "1",
                      "boost": 4
                    }
                  }
                },
                {
                  "match": {
                    "field_7": {
                      "query": "Query string",
                      "fuzziness": "1",
                      "boost": 1
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    },
    "from": "0",
    "size": "10"
  }
}
1 Like

Thank you for the answer. How can I use this template with different dates in this case?

Here I have a condition. If doc has a field 'Date' than it needs to be filtered by the range above. If doc doesn't have field 'Date' it must be added in the resulset too.

you can change here:

"params": {
    "query_string": "Query string",
    "gte": "2023-05-17", <--
    "lte": "2100-01-01" <--
  }

Do you mean that there are docs with the Date field and others not?

Yes, some docs may not have this field

You can add the snippet below in the "must" clause.

 {
          "bool": {
            "should": [
              {
                "range": {
                  "date": {
                    "gte": "2023-01-01",
                    "lte": "2023-01-03"
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "date"
                      }
                    }
                  ]
                }
              }
            ]
          }
        },

Can you, please, show how the whole query will look like?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.