# Aggregating on a nested field

**URL:** <https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537>\
**Category:** Elasticsearch\
**Created:** [October 15, 2019, 12:35am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537 "2019-10-15T00:35:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagannathan\_rajagopa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jagannathan_rajagopa/32/55882_2.png) [@jagannathan\_rajagopa](https://discuss.elastic.co/u/jagannathan_rajagopa)\
**Post date:** [October 15, 2019, 12:35am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/1 "2019-10-15T00:35:26Z")

</div>

I have an elastic search schema as below.

```auto
{
  "mappings": {
    "properties": {
      "process_id": {
        "type": "keyword"
      },
      "user_info": {
        "type": "nested",
        "properties": {
          "first_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "last_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "place": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
      

```

**2. I have added the below documents to the index.**

```auto
POST processes/_bulk 
  {"index":{"_id":1}}
{"process_id": "123", "user_info": [{"first_name": "A", "last_name": "B","place":"London"},{"first_name": "C", "last_name": "D","place":"Moscow"}]}
{"index":{"_id":2}}
{"process_id": "123", "user_info": [{"first_name": "C", "last_name": "B","Place":"Delhi"},{"first_name": "A", "last_name": "D","Place":"Bangalore"}]}

```

**3. I wanted to search for process\_id as 123,** first\_name as A and last\_name as B, and and aggregate on Place of the user\_info. I am trying to do it as below.

```auto
{ "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "process_id:123"
          }
        },
        {
          "nested": {
            "path": "user_info",
            "query": {
              "query_string": {
                "query": "user_info.first_name:A AND user_info.last_name:B"
              }
            }
          }
        }
      ]
    } }, "aggs": {
    "user_info": {
      "nested": {
        "path": "user_info"
      },
      "aggs": {
        "user_info.place": {
          "terms": {
            "field": "user_info.place"
          }
        }
      }
    } } }

```

The result returns the first document correctly. However, the aggregate on the field place returns both London and Moscow. However, I want London to be returned as the output. I am not sure how to proceed with this. I have tried Nested Filter Search Aggregation as well but no success on the same.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [October 15, 2019, 7:21am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/2 "2019-10-15T07:21:50Z")

</div>

Hello,  
You need to apply filter aggregation on nested level to sweep Moscow out.

---

<div class="post-metadata">

**Author:** ![jagannathan\_rajagopa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jagannathan_rajagopa/32/55882_2.png) [@jagannathan\_rajagopa](https://discuss.elastic.co/u/jagannathan_rajagopa)\
**Post date:** [October 15, 2019, 9:27am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/3 "2019-10-15T09:27:30Z")

</div>

Can you please elaborate a bit more?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [October 15, 2019, 10:55am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/4 "2019-10-15T10:55:59Z")

</div>

Pls check

> <https://stackoverflow.com/questions/40893401/elasticsearch-how-to-filter-nested-aggregation-bucket/41267385#41267385>

---

<div class="post-metadata">

**Author:** ![jagannathan\_rajagopa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jagannathan_rajagopa/32/55882_2.png) [@jagannathan\_rajagopa](https://discuss.elastic.co/u/jagannathan_rajagopa)\
**Post date:** [October 18, 2019, 3:57am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/5 "2019-10-18T03:57:37Z")

</div>

thank you! It worked

---

<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:** [November 15, 2019, 3:57am UTC](https://discuss.elastic.co/t/aggregating-on-a-nested-field/203537/6 "2019-11-15T03:57:45Z")

</div>

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