James-Jesse Drinkard
James-Jesse Drinkard

Reputation: 15703

How to combine term filters with a missing filter in Elasticsearch?

We are using Elasticsearch 1.6 and I have a working three term query that I need to modify with a stand alone working missing filter. Here is the current code:

The original term query with three entries

GET ...
{
  "query": {
    "nested": {
      "path": "MAIN_FIELD",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "MAIN_FIELD.ID": 1234
              }
            },
            {
              "term": {
                "MAIN_FIELD.OTHER_IND": "false"
              }
            },
            {
              "term": {
                "MAIN_FIELD.INDICATOR": "Y"
              }
            }
          ]
        }
      }
    }
  }

}

The stand alone missing query:

GET ...
{
    "query" : {
        "filtered" : {
            "filter" : {
                "missing" : { "field" : "MAIN_FIELD.OTHER_IND" }
            }
        }
    }
}

How do I change the term query from the first query:

"term": {
           "MAIN_FIELD.OTHER_IND": "false"
        }  

to use a missing filter?

Upvotes: 0

Views: 235

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

I think what you want is below:

{
   "query": {
      "nested": {
         "path": "MAIN_FIELD",
         "query": {
            "bool": {
               "must": [
                  {
                     "term": {
                        "MAIN_FIELD.ID": 1234
                     }
                  },
                  {
                     "filtered": {
                        "filter": {
                            "missing": {
                               "field": "MAIN_FIELD.OTHER_IND"
                            }
                        }
                     }
                  },
                  {
                     "term": {
                        "MAIN_FIELD.INDICATOR": "Y"
                     }
                  }
               ]
            }
         }
      }
   }
}

Upvotes: 1

Related Questions