I’m trying to perform an elasticsearch query with multiple fields specified. Here is some pseudo code which hopefully illustrates my intention:
"query" : {
"query_string" : { "query" : "william" }
},
"filter" : {
"missing" : { "field" : "membership_expires_on" }
},
"filter" : {
"missing" : { "field" : "gender" }
},
"filter" : {
"terms" : { "status" : "p"}
},
"filter" : {
"terms" : { "unit_id" : "4"}
}
I’m trying to AND all these filters together, but from the elasticsearch doc I can’t figure out how to put these specific filters into a single query. Any help would be greatly appreciated!
UPDATE: Here is what I have so far:
{
"from": 0,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"missing": {
"field": "membership_expires_on"
}
},
{
"term": {
"status": "p"
}
},
{
"missing": {
"field": "gender"
}
},
{
"term": {
"unit": "4"
}
}
]
}
},
"query": {
"query_string": {
"query": "william"
}
}
}
},
"size": 10
}
But this returns no results, so I’m doing something wrong. Am I wielding the bool filter with the array inside in an illegal manner?
Update 2: I’ve confirmed that the JSON above indeed is a valid query, so there’s probably something amiss with my data/index. That will be a question for another day 🙂
Have a look at the BoolFilter. It should help.