I have a ES index with a nested field called “relations”. In the mapping, it is defined as:
"relations": {
"type": "nested",
"properties": {
"primaryWordIndex": {
"type": "long"
},
"secondaryWordIndex": {
"type": "long"
},
"score": {
"type": "float"
}
}
},
In my index I have 3 types of documents:
- Documents that do not have the “relations” field at all
- Documents that have a “relations” field filled with a empty array
- Documents that have a properly filled “relations” field.
I now want to query for the first and second occurrence. I was using the following query:
GET /myindex/_search
{
"query":{
"bool":{
"must_not":[
{
"nested":{
"path":"relations",
"query":{
"exists":{
"field":"relations"
}
}
}
}
]
}
}
}
But that returns both records that do not have a relation fields and records that have an empty array as relations field.
I tried a filter like this to get records where the "relations" field is a empty array:
"filter" : {
"script" : {
"script" : "doc['relations'].values.length == 0"
}
}
but that returned. error:
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "No field found for [relations] in mapping"
}
Any idea how I should change my query?
I'm using Elasticsearch 7.17.0