# Issue on writting elasticsearch queries for nested field

**URL:** <https://discuss.elastic.co/t/issue-on-writting-elasticsearch-queries-for-nested-field/188926>\
**Category:** Elasticsearch\
**Created:** [July 4, 2019, 2:13pm UTC](https://discuss.elastic.co/t/issue-on-writting-elasticsearch-queries-for-nested-field/188926 "2019-07-04T14:13:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kahoni](https://avatars.discourse-cdn.com/v4/letter/k/c37758/32.png) [@kahoni](https://discuss.elastic.co/u/kahoni)\
**Post date:** [July 4, 2019, 2:13pm UTC](https://discuss.elastic.co/t/issue-on-writting-elasticsearch-queries-for-nested-field/188926/1 "2019-07-04T14:13:04Z")

</div>

Hi everyone,

I have an issue writing elasticsearch queries with nested field. I think you could help me because I'm the only one using Elastic in my company and I don't know how to solve my problem alone.

Here are how my ES docs look like in this index (very simplified version)  
\> {

> ```
> "_index":"index_test",
> "_type":"type_test",
> "_id":"1",
> "_score":1.0,
> "_source":{
> "category":1,
> "value_period":[
> {
> "value":5.4,
> "month":"2019-01"
> }
> ],
> "client_id":"80",
>  
> }
> },
> {
> "_index":"index_test",
> "_type":"type_test",
> "_id":"2",
> "_score":1.0,
> "_source":{
> "category":1,
> "value_period":[
> {
> "value":10.0,
> "month":"2019-02"
> }
> ],
> "client_id":"80",
>  
> }
> }{
> "_index":"index_test",
> "_type":"type_test",
> "_id":"2",
> "_score":1.0,
> "_source":{
> "category":2,
> "value_period":[
> {
> "value":20.0,
> "month":"2019-02"
> }
> ],
> "client_id":"80",
> 
> }
> }
> 
> ```

Here is the question:  
I need to write 4 queries for four different use case:  
2 simple queries : "sum per month" and "sum per category"

- 1st: date\_histogram aggregation (on nested field "value\_period.month") and sum aggregation (on value\_period.value) in order to get a simple sum by month (1 value per month).
- 2nd: term aggregation (on field "category") and sum aggregation (on value\_period.value) in order to get a simple value per category

2 queries a little more complex: "sum per month per category" and "sum per category per month"

- 3rd: date\_histogram aggregation (on nested field "value\_period.month"), term aggregation (on field "category") and sum aggregation (on value\_period.value) in order to get sum per month per category
- 4th: term aggregation (on field "category"), date\_histogram aggregation (on nested field "value\_period.month") and sum aggregation (on value\_period.value) in order to get sum per category per month

For the 1st case (simple value per month) I have already written the query and it works:

> ```
> {
> "aggs":{
> "nested_categories":{
> "nested":{
> "path":"value_period"
> },
> "aggs":{
> "date_agg_interval":{
> "date_histogram":{
> "field":"value_period.month",
> "interval":"1M",
> "format":"yyyy-MM",
> "min_doc_count":0
> },
> "aggs":{
> "agg_sum":{
> "sum":{
> "field":"value_period.value"
> }
> }
> }
> }
> }
> }
> }
> }
> 
> ```

I really want to thanks in advance for any people that will help me out and consider my request.  
PS: excuse my level of english I'm french.

---

<div class="post-metadata">

**Author:** ![kahoni](https://avatars.discourse-cdn.com/v4/letter/k/c37758/32.png) [@kahoni](https://discuss.elastic.co/u/kahoni)\
**Post date:** [July 8, 2019, 7:43am UTC](https://discuss.elastic.co/t/issue-on-writting-elasticsearch-queries-for-nested-field/188926/2 "2019-07-08T07:43:23Z")

</div>

Can anybody help me ? Or just give me some tricks that could help me for the double aggregation. Thank you very much

---

<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:** [August 5, 2019, 7:43am UTC](https://discuss.elastic.co/t/issue-on-writting-elasticsearch-queries-for-nested-field/188926/3 "2019-08-05T07:43:31Z")

</div>

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