# ElasticSeach Query Error in Aggregation

**URL:** <https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490>\
**Category:** Elasticsearch\
**Created:** [July 29, 2015, 2:23pm UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490 "2015-07-29T14:23:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 29, 2015, 2:23pm UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/1 "2015-07-29T14:23:48Z")

</div>

Hi Guys,

I am currently trying to do aggregation by my "custom\_id" field over the respective "x\_count" for each document. There can only be few distinct values in my "custom\_id" field.

GET /index1/table1/\_search  
{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "\*",  
"analyze\_wildcard": true  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"start\_timestamp": {  
"gte": 1430483327935,  
"lte": 1432988927935  
}  
}  
}  
],  
"must\_not": []  
}  
}  
}  
},  
"aggs": {  
"custom\_id": {  
"terms": {  
"field": "custom\_id",  
},  
"aggs": {  
"spe": {  
"sum": {  
"field": "x\_count"  
}  
}  
}  
}  
}  
}

As i am still quiet new to Elasticsearch , i might be missing something obvious .

Thanks.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 29, 2015, 2:40pm UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/2 "2015-07-29T14:40:32Z")

</div>

What issue are you getting with the above query? If there is an error in the response can you please paste the error here.

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 30, 2015, 7:16am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/3 "2015-07-30T07:16:50Z")

</div>

Hi

Thanks for the reply!

The error i get was a huge but i think this might be the relevant part.

- Error:

nested: JsonParseException[Unexpected character ('}' (code 125)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@4c8c5b8e; line: 32, column: 2]]; }]",

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 30, 2015, 8:08am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/4 "2015-07-30T08:08:40Z")

</div>

> [@Aditya\_Patel](#):
>
> "field": "custom\_id",

you need to remove the comma at the end of this line in your request to make this valid json.

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 30, 2015, 10:27am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/5 "2015-07-30T10:27:57Z")

</div>

Hi Colin,

Thanks it worked !

Now suppose if i want to aggregate along with range (currently the query is on date range) and also on a specific field type (ex. type\_a = 23).

GET /index1/table1/\_search  
{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "\*",  
"analyze\_wildcard": true  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"start\_timestamp": {  
"gte": 1430483327935,  
"lte": 1432988927935  
}  
}  
}  
],  
"must": [  
{"term": {  
"type\_a":23  
}  
}  
],  
"must\_not": []  
}  
}  
},  
"aggs": {  
"custom\_id": {  
"terms": {  
"field": "custom\_id"  
},  
"aggs": {  
"spe": {  
"sum": {  
"field": "x\_count"  
}  
}  
}  
}  
}  
}

- Error  
nested: ElasticsearchParseException[Expected field name but got START\_OBJECT "aggs"]; }]",  
"status": 400

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 30, 2015, 11:30am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/6 "2015-07-30T11:30:21Z")

</div>

This is again a JSON validation problem. you should use a json validator to fix these issues (e.g. [http://jsonlint.com/](http://jsonlint.com/))

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 30, 2015, 11:50am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/7 "2015-07-30T11:50:05Z")

</div>

Hi Collin,

Thanks for the link and answering my naive questions so quickly 😃

it was showing me an error for "}" was missing when i added the "}" at the end, it became a valid JSON but still i am getting the same error:

-Error

nested: ElasticsearchParseException[Expected field name but got START\_OBJECT "aggs"]; }]",

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 30, 2015, 11:51am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/8 "2015-07-30T11:51:56Z")

</div>

> [@Aditya\_Patel](#):
>
> },  
> "aggs": {

You need to add the brace before the comma here instead

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 31, 2015, 5:52am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/9 "2015-07-31T05:52:20Z")

</div>

Thanks ! it worked.

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [August 5, 2015, 6:32am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/10 "2015-08-05T06:32:20Z")

</div>

Hi @colings86

If i want to print more information related to "custom\_id" which is unique for all documents which have that particular custom\_id. Can i get that by adding some arguments in the same query. I tried

"\_include" but its not working.

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [August 5, 2015, 7:58am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/11 "2015-08-05T07:58:23Z")

</div>

It depends what extra information you wanted to print but the general method would be to add more aggregations alongside your `spe` aggregation

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [August 6, 2015, 5:54am UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/12 "2015-08-06T05:54:22Z")

</div>

Hi Collin,

I was able to get it but i ended up using something which i think might be weird

Not a complete query:

"SpecificInfo": {  
"top\_hits": {  
"sort": [  
{  
"start\_timestamp": {  
"order": "desc"  
}  
}  
],  
"\_source": {  
"include": [  
"age\_max"  
,"age\_min"  
]  
},  
"size" : 1  
}  
}

Another question, i had in mind was suppose i wanted another metric which was a division of my aggregators (in this example "spe" and lets call another aggregator "gain") . I wanted to add another metric which was division of spe/gain, how would i got about it.

I might be able to use "bucket\_path" but don't have a concrete path to it.

Thanks again!

---

<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 5, 2017, 11:57pm UTC](https://discuss.elastic.co/t/elasticseach-query-error-in-aggregation/26490/13 "2017-07-05T23:57:11Z")

</div>


