I have a field gender
which can have three values:
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?
{
"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.
Comments