Matching null in should query in Elasticsearch 2.3.0

Animesh Pandey

I have a field gender which can have three values:

  • MALE
  • FEMALE
  • null

The mapping of gender is:

"gender": {
    "type": "string",
    "index": "not_analyzed"
}

To match null I am following this.

I used this query to get the documents with gender as null:

{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "gender"
                }
            }
        }
    }
}

I want to build a should query so that the search results could return any documents having the gender field:

{
  "query": {
    "bool": {
      "should": {
        "bool": {
          "should": [
            {
              "match": {
                "gender": {
                  "query": "MALE",
                  "type": "boolean"
                }
              }
            },
            {
              "match": {
                "gender": {
                  "query": "FEMALE",
                  "type": "boolean"
                }
              }
            }
          ],
          "must_not": {
            "exists": {
              "field": "gender"
            }
          }
        }
      }
    }
  }
}

The query above gives me 0 hits. How should I construct the right query for this?

Andrei Stefan
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "gender": {
              "query": "MALE",
              "type": "boolean"
            }
          }
        },
        {
          "match": {
            "gender": {
              "query": "FEMALE",
              "type": "boolean"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "gender"
              }
            }
          }
        }
      ]
    }
  }
}

or if I didn't understand the question well and in fact you want to differentiate between gender completely missing from a document or the field being there, but having null, you need something else, that requires changing the mapping slightly:

    "gender": {
      "type": "string",
      "index": "not_analyzed",
      "null_value": "_null_"
    }

And in this case the query is:

  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "gender": {
              "query": "MALE"
            }
          }
        },
        {
          "match": {
            "gender": {
              "query": "FEMALE"
            }
          }
        },
        {
          "term": {
            "gender": "_null_"
          }
        }
      ]
    }
  }

In this case, if you index:

{"index":{}}
{"gender":"MALE"}
{"index":{}}
{"gender":"FEMALE"}
{"index":{}}
{"gender":null}
{"index":{}}
{"gender2":"MALE"}
{"index":{}}
{"gender":"whatever"}

it will give you back:

     {
        "_score": 0.2826735,
        "_source": {
           "gender": "MALE"
        }
     },
     {
        "_score": 0.2826735,
        "_source": {
           "gender": null
        }
     },
     {
        "_score": 0.021688733,
        "_source": {
           "gender": "FEMALE"
        }
     }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elasticsearch should not operation query

From Dev

Elasticsearch should not operation query

From Dev

Elasticsearch Multiple Prefix query OR Matching

From Dev

Finding ElasticSearch records matching empty and null values

From Dev

Elasticsearch one record for one matching query

From Dev

Elasticsearch DSL query - Get all matching results

From Java

Elasticsearch difference between MUST and SHOULD bool query

From Dev

Elasticsearch not sure if filter or query should be first

From Dev

ElasticSearch bool query combined with should and filter

From Dev

SQL query count = 0/null

From Dev

Query returns null instead of 0

From Dev

Null entering as 0 in the below query

From Dev

oracle sql query finding rows with multiple values in 3rd column matching columns 1 and 2

From Dev

Matching a string with a regex gives null even though it should match

From Dev

Matching a string with a regex gives null even though it should match

From Dev

How to delete document matching a query using official elasticsearch nodejs client?

From Dev

Query for : How many elements of an array are matching in a document attribute in ElasticSearch

From Dev

How to delete document matching a query using official elasticsearch nodejs client?

From Dev

Boolean search fuzzy query for Elasticsearch - Unexpected result - "Word1" AND "Word2" ~3

From Dev

Elasticsearch: Get report of unmatched should elements in a bool query

From Dev

Using minimum_should_match in filtered elasticSearch query

From Dev

How should query does it work ? ElasticSearch Java API

From Dev

MySql query monthly return 0 if null

From Java

Replacing NULL with 0 in a SQL server query

From Dev

Get Count 0 instead of NULL MySQL Query

From Dev

MySql query monthly return 0 if null

From Dev

How can I get the score of the underlying matching query in a bool query in ElasticSearch?

From Dev

How to query NOT NULL with Doctrine 2?

From Dev

Limit query with elasticsearch using Yii2

Related Related

  1. 1

    Elasticsearch should not operation query

  2. 2

    Elasticsearch should not operation query

  3. 3

    Elasticsearch Multiple Prefix query OR Matching

  4. 4

    Finding ElasticSearch records matching empty and null values

  5. 5

    Elasticsearch one record for one matching query

  6. 6

    Elasticsearch DSL query - Get all matching results

  7. 7

    Elasticsearch difference between MUST and SHOULD bool query

  8. 8

    Elasticsearch not sure if filter or query should be first

  9. 9

    ElasticSearch bool query combined with should and filter

  10. 10

    SQL query count = 0/null

  11. 11

    Query returns null instead of 0

  12. 12

    Null entering as 0 in the below query

  13. 13

    oracle sql query finding rows with multiple values in 3rd column matching columns 1 and 2

  14. 14

    Matching a string with a regex gives null even though it should match

  15. 15

    Matching a string with a regex gives null even though it should match

  16. 16

    How to delete document matching a query using official elasticsearch nodejs client?

  17. 17

    Query for : How many elements of an array are matching in a document attribute in ElasticSearch

  18. 18

    How to delete document matching a query using official elasticsearch nodejs client?

  19. 19

    Boolean search fuzzy query for Elasticsearch - Unexpected result - "Word1" AND "Word2" ~3

  20. 20

    Elasticsearch: Get report of unmatched should elements in a bool query

  21. 21

    Using minimum_should_match in filtered elasticSearch query

  22. 22

    How should query does it work ? ElasticSearch Java API

  23. 23

    MySql query monthly return 0 if null

  24. 24

    Replacing NULL with 0 in a SQL server query

  25. 25

    Get Count 0 instead of NULL MySQL Query

  26. 26

    MySql query monthly return 0 if null

  27. 27

    How can I get the score of the underlying matching query in a bool query in ElasticSearch?

  28. 28

    How to query NOT NULL with Doctrine 2?

  29. 29

    Limit query with elasticsearch using Yii2

HotTag

Archive