Giox
Giox

Reputation: 5143

Elasticsearch query for getting records with null values in a field

I'm trying to get the result of all the records where the field iso3 contains some values and null, but I always get the following error:

{
    "code": 400,
    "error": "RequestError(400, 'parsing_exception', '[or] query malformed, no start_object after query name')"
}

this is the query:

{
  "query": {
      "bool": {
            "should": [
    {"term":{"iso3":"afg"}},
    {"term":{"iso3":"idn"}},
    {"term":{"iso3":"bgd"}},
        {
           "or": [
               {"term": { "iso3": "" } },
               {"term": { "iso3": null}}
            ]
        }
    ]
      }

  },
  "size": 20
}

so this part is wrong, but I cant understand why

        {
           "or": [
               {"term": { "iso3": "" } },
               {"term": { "iso3": null}}
            ]
        }

Upvotes: 0

Views: 905

Answers (1)

Val
Val

Reputation: 217594

There is no or query, but you can achieve what you need with the following query:

{
  "query": {
    "bool": {
      "minimum_should_match": true,
      "should": [
        {
          "term": {
            "iso3": "afg"
          }
        },
        {
          "term": {
            "iso3": "idn"
          }
        },
        {
          "term": {
            "iso3": "bgd"
          }
        },
        {
          "term": {
            "iso3": ""
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "iso3"
              }
            }
          }
        }
      ],
      "filter": [
        {
          "exists": {
            "field": "iso2"
          }
        }
      ]
    }
  },
  "size": 20
}

Upvotes: 1

Related Questions