# Aggregations with filter

**URL:** <https://discuss.elastic.co/t/aggregations-with-filter/18683>\
**Category:** Elasticsearch\
**Created:** [July 15, 2014, 6:57pm UTC](https://discuss.elastic.co/t/aggregations-with-filter/18683 "2014-07-15T18:57:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![esuser](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@esuser](https://discuss.elastic.co/u/esuser)\
**Post date:** [July 15, 2014, 6:57pm UTC](https://discuss.elastic.co/t/aggregations-with-filter/18683/1 "2014-07-15T18:57:23Z")

</div>

Hi All,

I am new to elastic search . I am trying below query in order to get  
results based on some where condition with group by and avg.

Query always returns zero hits, docs. If i remove terms , it is giving  
results.

This was working fine sometime back.After i deleted the index and created a  
new one, it is not working. Any pointers would help.

If I use fields which are not string as filter terms, it gives me proper  
results.

{  
"aggs": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"COLA": "NA"  
}  
},  
{  
"term": {  
"COLB": "CUSTOMER"  
}  
} ,  
{  
"range": {  
"SCORE": {  
"from": 0,  
"to": 90  
}  
}  
}  
]  
}  
} ,  
"aggs" : {  
"cust" : {  
"terms" : {  
"field" : "COLC"  
},  
"aggs" : {  
"avg\_score" : { "avg" : { "field" : "SCORE" } }  
}  
}  
}  
}  
}  
}

Thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 16, 2014, 8:44am UTC](https://discuss.elastic.co/t/aggregations-with-filter/18683/2 "2014-07-16T08:44:15Z")

</div>

Hi,

A very likely cause for this issue is that by deleting and recreating your  
index, you lost your mappings. Quite likely, you want to set your COLA,  
COLB and COLC fields index=not\_analyzed:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Tue, Jul 15, 2014 at 8:57 PM, esuser [subacini@gmail.com](mailto:subacini@gmail.com) wrote:

> Hi All,
> 
> I am new to Elasticsearch . I am trying below query in order to get  
> results based on some where condition with group by and avg.
> 
> Query always returns zero hits, docs. If i remove terms , it is giving  
> results.
> 
> This was working fine sometime back.After i deleted the index and created  
> a new one, it is not working. Any pointers would help.
> 
> If I use fields which are not string as filter terms, it gives me proper  
> results.
> 
> {  
> "aggs": {  
> "filtered": {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "COLA": "NA"  
> }  
> },  
> {  
> "term": {  
> "COLB": "CUSTOMER"  
> }  
> } ,  
> {  
> "range": {  
> "SCORE": {  
> "from": 0,  
> "to": 90  
> }  
> }  
> }  
> ]  
> }  
> } ,  
> "aggs" : {  
> "cust" : {  
> "terms" : {  
> "field" : "COLC"  
> },  
> "aggs" : {  
> "avg\_score" : { "avg" : { "field" : "SCORE" } }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Thanks.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1\_qE9vKTESR\_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1_qE9vKTESR_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![esuser](https://avatars.discourse-cdn.com/v4/letter/e/f9ae1b/32.png) [@esuser](https://discuss.elastic.co/u/esuser)\
**Post date:** [July 16, 2014, 5:29pm UTC](https://discuss.elastic.co/t/aggregations-with-filter/18683/3 "2014-07-16T17:29:22Z")

</div>

Thank you Adrien, you are right it was mapping issue. I set analyzer to  
keyword and it started working fine as expected.

On Wed, Jul 16, 2014 at 1:44 AM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Hi,
> 
> A very likely cause for this issue is that by deleting and recreating your  
> index, you lost your mappings. Quite likely, you want to set your COLA,  
> COLB and COLC fields index=not\_analyzed:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#string)
> 
> On Tue, Jul 15, 2014 at 8:57 PM, esuser [subacini@gmail.com](mailto:subacini@gmail.com) wrote:
> 
> > Hi All,
> > 
> > I am new to Elasticsearch . I am trying below query in order to get  
> > results based on some where condition with group by and avg.
> > 
> > Query always returns zero hits, docs. If i remove terms , it is giving  
> > results.
> > 
> > This was working fine sometime back.After i deleted the index and created  
> > a new one, it is not working. Any pointers would help.
> > 
> > If I use fields which are not string as filter terms, it gives me proper  
> > results.
> > 
> > {  
> > "aggs": {  
> > "filtered": {  
> > "filter": {  
> > "bool": {  
> > "must": [  
> > {  
> > "term": {  
> > "COLA": "NA"  
> > }  
> > },  
> > {  
> > "term": {  
> > "COLB": "CUSTOMER"  
> > }  
> > } ,  
> > {  
> > "range": {  
> > "SCORE": {  
> > "from": 0,  
> > "to": 90  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > } ,  
> > "aggs" : {  
> > "cust" : {  
> > "terms" : {  
> > "field" : "COLC"  
> > },  
> > "aggs" : {  
> > "avg\_score" : { "avg" : { "field" : "SCORE" } }  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Thanks.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/7b1cb3b5-7276-4ed8-a668-3cc7c285fbf0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/udEUL5bJAzo/unsubscribe](https://groups.google.com/d/topic/elasticsearch/udEUL5bJAzo/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1\_qE9vKTESR\_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1_qE9vKTESR_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1\_qE9vKTESR\_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j7%2BvsQR%3D--C5w1b1_qE9vKTESR_LxpHy4ZdsmUOyRJ-mQ%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAJfX5E27gVAJ52kPniF0Ly12vvPFLSW1CS9UzyGFe1hKLtU3ig%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJfX5E27gVAJ52kPniF0Ly12vvPFLSW1CS9UzyGFe1hKLtU3ig%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:15am UTC](https://discuss.elastic.co/t/aggregations-with-filter/18683/4 "2017-07-06T01:15:23Z")

</div>


