0
votes

I have following elasticsearch schema, where leaves is a nested field:

{
studentId:123,
studentName:'abc',
leaves:[
 {
  type:'casual',
  date:'2012-12-12'
 },
 {
  type:'sick',
  date:'2012-10-08'
 }
]
}

I want to filter/query the students having at least 2 holidays where holiday type is 'casual'.Just filter the documents, no aggregation required. Have seen the question below which is old and uses deprecated 'filtered' query. elastic search filter by documents count in nested document

My Elasticsearch version is 7.5

1
I would suggest to additionally index a new field that contains the number of elements in the nested array. It will be easier to build your query - Val
thanks @Val is there any other way of achieving this in query, maybe through script or anything else? - Kamboh

1 Answers

0
votes

Here is the reference DOC

GET /index/_search
{
        "query": {
            "bool": {
                "must": {
                    "script": {
                        "script": {
                            "inline": "doc['leaves'].values.length > 1 "

                        }
                    }
                }
            }
        }
    }