Combine Filter the records from nested query - Elastic Search

I am having nested array fields, I need to query and filter the records for that. Sample payload

"test":[
  "name":[
     {
       "name": "vanaraj",
       "Age" : 26
     },
     {
       "name": "vanaraj",
       "Age" : 10
     },
     {
       "name": "ranjit",
       "Age" : 26
     },

    ]

]

Mapping :

{  
   "mappings":{  
      "properties":{  
         "test":{  
            "properties":{  
               "name":{  
                  "type":"nested",               
                  "properties":{  
                     "Age":{  
                        "type":"long"
                     },
                     "name":{  
                        "type":"text",
                        "fields":{  
                           "keyword":{  
                              "type":"keyword",
                              "ignore_above":256
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

I am having nested array fields, I need to query and filter the records for that. Sample

"test":[
  "name":[
     {
       "name": "vanaraj",
       "Age" : 26
     },
     {
       "name": "vanaraj",
       "Age" : 10
     },
     {
       "name": "ranjit",
       "Age" : 26
     },

    ]

]

Here how I need the query for below conditions,

  1. Where Name is equal to both ["vanaraj","ranjit"] to fetch
  2. Add condition where Age > 25 for only "vanaraj"
    Remaining for ranjit it should fetch all the records, it should not the check the condition where Age >25.

I need a query like below, but it is not working.

{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "data.test.name",
            "query": {
              "bool": {
                "filter": [
                  {
                    "terms": {
                      "data.test.name.name": ["vanaraj","ranjit"]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "data.test.name",
            "query": {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "data.test.name.name": "vanaraj"
                    }
                  },
                  {
                    "range": {
                       "data.test.name.Age": {
                                            "gt": 25
                        }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.