elasticsearch – Elastic Search searching within arrays with should clause

I have an item in my index which contains a field which is an array. This array contains items with date fields “from” and “to”. “From” is always set but “to” can be empty. With my query I don’t seem to be able to find this specific item.

{
    id: 1234,
    items: [
        {
            from: 1993-01-01,
            to: 2007-11-12
        },
        {
            from: 2007-11-13,
        },
    ]
}

This is my query :

{
   "bool": {
     "must": [
       {
         "bool": {
           "should": [
             {
               "range": {
                 "to": {
                   "gte": 2019
                 }
               }
             },
             {
               "bool": {
                 "must_not": [
                   {
                     "exists": {
                       "field": "to"
                     }
                   }
                 ]
               }
             }
           ]
         }
       },
       {
         "range": {
           "from": {
             "lte": "2023-12-31T23:59:59.999"
           }
         }
       }
     ],
   }
 }

I was hoping, that my item from above would show up in the search results.

It would seem that the OR/SHOULD clauses are evaluated to false – each of
them for another element in the array.
E.g. :

{
    "range": {
        "to": {
            "gte": 2019
        }
    }
}

evaluates to false for the first item and not applicable for the second.

{
    "bool": {
        "must_not": [
            {
                "exists": {
                    "field": "to"
                }
            }
        ]
    }
}

evaluates to false for the first item.

So false OR false => false – no item is found.

If this thesis is correct, how to tell ES that if one of the items in the array matches one of the conditions, the item should be in the result set.

Read more here: Source link