Elasticsearch query to compute boolean conditions

We want to write an Elasticsearch search template something like following :

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "variable1": "{{value1}}"
              }
            },
            {
              "or" : [
                {
                  "term" : {
                    "variable2" : "{{value2}}"
                  } 
                },
                {
                  "{{value2}}" is null
                }
                ]
            }
          ]
        }
      }
    }
  }
}

I am aware that the part where I wrote "{{value2}}" is null is invalid syntax. The result we want to achieve is that if the {{value2}} param passed to the search template is null then the search should only be filtered based on variable1 . So to achieve this kind of result how can I frame the query in ES ?

Hi @dadoonet . I see that you have a lot of contributions here so I thought to tag you directly here. Sorry for that. But do you have an idea how can I solve the problem I mentioned above?

Not sure. may be you can find something useful there: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_conditional_clauses

Thanks @dadoonet . I have already checked it but did not find my solution there. But thanks for the reply :slight_smile:

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