I have a simple search using scripts:
{
"query": {
"bool": {
"filter": [
{
"exists": {
"field": "fieldX"
}
},
{
"exists": {
"field": "fieldY"
}
}
]
}
},
"aggs": {
"groupBy": {
"terms": {
"script": {
"source": "doc['fieldX.keyword'].value+','+doc['fieldY.keyword'].value"
}
}
}
}
}
Groups are the result of this search:
"buckets" : [
{
"key" : "valueX1,valueY1",
"doc_count" : 165
},
{
"key" : "valueX2,valueY2",
"doc_count" : 45
}
]
The main problem here is: If all the fields (fieldX and fieldY) in documents exist - everything will be fine. If at least one field is missing, the search will return nothing.
I tried to rewrite this to normal search using aggs, terms, field but unsuccessfully.
Any idea how I can rewrite this search to keep the original result but avoid the described problem?