Elastic serch multiple filters for dynamically selecting filters

Hi, I am working with elasticsearch application for one of my application. My application need is below-

I have mulitple filters like location, type, qualification etc.. i have created query for this in which i am passing these filter values. so if location filter is not selected its value will be null and similar to others. therefoere, when i am applying these filters all together using terms query filter, it is giving me no result and this is because value of one or filter is going as null because those are not selected. i want that if any filter value is null or not selected i don't want to apply that filter so how can i achieve this in a common query. don't we have any such thing in filter query if filter value coming as null then filter should be run-

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "terms": {
            "preferredLocation.city": [
              "delhi"
            ]
          }
        },
        {
          "terms": {
            "qualificationRequired": [
              ""
            ]
          }
        },
        {
          "term": {
            "jobType": ""
          }
        },
        {
          "term": {
            "deliveryMedium": ""
          }
        }
      ]
    }
  }
}

You should ask your application to do that.
For the example you wrote, you should send instead:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "preferredLocation.city": [
              "delhi"
            ]
          }
        }
      ]
    }
  }
}

Thanks for your reply. so what i understood form your point that we cannot manage it the way i wanted like to have a common query ?. Different query needs to be hit on elasticsearch for this based on which filters are selected.

Probably not in the way you want.
You can look at search templates though. Read https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_conditional_clauses

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