I would like to query my elasticsearch index in order to retrieve the documents that don't contain a specific value in an array. For instance, if my query is :
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"facets": {}
}
And the dataset :
{
"took": 1,
"hits": {
"total": 1,
"hits": [
{
"_index": "product__1434374235336",
"_type": "product",
"_id": "AU33Xeny0K4pKlL-a7sr",
"_source": {
"interdictions": ["S0P","SK3"],
"code": "foo"
}
},
{
"_index": "product__1434374235336",
"_type": "product",
"_id": "AU33Xeny0K4pKlL-a7sr",
"_source": {
"interdictions": ["S0P","S2V","SK3"],
"code": "bar"
}
}
]
}
}
The objective is to exclude each product that contains the "S2V" interdiction. I initially thought of using a missing filter :
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"must_not": [],
"should": []
}
},
"filter": {
"missing": {
"terms": {
"interdictions": [
"S2V"
]
}
}
},
"from": 0,
"size": 10,
"sort": [],
"facets": {}
}
But elasticsearch fails to parse the query : QueryParsingException[[product__1434374235336] [missing] filter does not support [interdictions]]; }]",. I then tried with a must_not :
{
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"must_not" : {
"terms" : {
"interdictions" : ["S2V"]
}
}
}
},
"from": 0,
"size": 10
}
But the output is incorrect since it returns a product with the S2V interdictions.
So... What is the correct way to do this ?
Thanks !
matchinstead ofterms, so the analyzer will be applied forS2Vwhen querying. View more here (Why doesn’t the term query match my document?) : elastic.co/guide/en/elasticsearch/reference/current/…