# Aggregation on Nested fields with prefix query

**URL:** <https://discuss.elastic.co/t/aggregation-on-nested-fields-with-prefix-query/85921>\
**Category:** Elasticsearch\
**Created:** [May 16, 2017, 11:34am UTC](https://discuss.elastic.co/t/aggregation-on-nested-fields-with-prefix-query/85921 "2017-05-16T11:34:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jai\_Prakash\_Bhardwaj](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Jai\_Prakash\_Bhardwaj](https://discuss.elastic.co/u/Jai_Prakash_Bhardwaj)\
**Post date:** [May 16, 2017, 11:34am UTC](https://discuss.elastic.co/t/aggregation-on-nested-fields-with-prefix-query/85921/1 "2017-05-16T11:34:01Z")

</div>

Please help me with the following issue:

```auto
{
  "size": 0, 
       "query": {
        "nested":{
"path": "locations",
"query":{
"multi_match" : {
"fields" : ["locations.CityName"],
"query" : "CH",
"type": "phrase_prefix"
}},
"inner_hits" : {
"_source": { "includes": ["locations.CityName"] }}
}}
         
       , 
    "aggs" : {
        "Location_Name" : {
            "nested" : {
                "path" : "locations"
            },
            "aggs" : {
                "City_Name" : { "terms" : { "field" : "locations.CityName.keyword" } }
            }
        }
    }
}

```

So, I should get all city with CH, but I get:

```auto
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "Location_Name": {
      "doc_count": 34,
      "City_Name": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 14,
        "buckets": [
          {
            "key": "CHILLICOTHE",
            "doc_count": 4
          },
          {
            "key": "SIOUX CITY",
            "doc_count": 4
          }

```

Please help me in the missed logic.

Best,  
J

---

<div class="post-metadata">

**Author:** ![Jai\_Prakash\_Bhardwaj](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@Jai\_Prakash\_Bhardwaj](https://discuss.elastic.co/u/Jai_Prakash_Bhardwaj)\
**Post date:** [May 22, 2017, 6:07am UTC](https://discuss.elastic.co/t/aggregation-on-nested-fields-with-prefix-query/85921/2 "2017-05-22T06:07:14Z")

</div>

Do we have anybody who can help me in this please?

---

<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:** [June 19, 2017, 6:07am UTC](https://discuss.elastic.co/t/aggregation-on-nested-fields-with-prefix-query/85921/3 "2017-06-19T06:07:15Z")

</div>

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