# Combine Filter the records from nested query - Elastic Search

**URL:** https://discuss.elastic.co/t/combine-filter-the-records-from-nested-query-elastic-search/184484
**Category:** Elasticsearch
**Created:** [June 6, 2019, 2:41am UTC](https://discuss.elastic.co/t/combine-filter-the-records-from-nested-query-elastic-search/184484 "2019-06-06T02:41:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![vnrj1995](https://avatars.discourse-cdn.com/v4/letter/v/e0b2c6/32.png) [@vnrj1995](https://discuss.elastic.co/u/vnrj1995)
#### Post date: [June 6, 2019, 2:41am UTC](https://discuss.elastic.co/t/combine-filter-the-records-from-nested-query-elastic-search/184484/1 "2019-06-06T02:41:02Z")

</div>

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

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

    ]

]

```

Mapping :

```auto
{  
   "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

```auto
"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.

```auto
{
  "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
                        }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 4, 2019, 2:41am UTC](https://discuss.elastic.co/t/combine-filter-the-records-from-nested-query-elastic-search/184484/2 "2019-07-04T02:41:16Z")

</div>

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