# How to use aggregations?

**URL:** <https://discuss.elastic.co/t/how-to-use-aggregations/17132>\
**Category:** Elasticsearch\
**Created:** [April 22, 2014, 6:39pm UTC](https://discuss.elastic.co/t/how-to-use-aggregations/17132 "2014-04-22T18:39:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)\
**Post date:** [April 22, 2014, 6:39pm UTC](https://discuss.elastic.co/t/how-to-use-aggregations/17132/1 "2014-04-22T18:39:37Z")

</div>

Is there a way to aggregate only the top 2 results based on \_score for docs  
with the same value in a certain field?

the hits before this aggregation would be like:

{  
"\_index":"myindex",  
"\_score":100,  
"\_source": {  
"myfield1": "i have a twin",  
"name":"fred"  
}  
},

{  
"\_index":"myindex",  
"\_score":50,  
"\_source": {  
"myfield1": "i have a twin",  
"name":"george"  
}  
},

{  
"\_index":"myindex",  
"\_score":10,  
"\_source": {  
"myfield1": "i have a twin",  
"name":"tom"  
}  
},

{  
"\_index":"myindex",  
"\_score":10,  
"\_source": {  
"myfield1": "i DONT have a twin",  
"name":"doug"  
}  
}

Then after this filter I want this ... tom removed because he has same  
value for myfield1, but the lowest score. Doug stays because he has a  
different value for myfield1.

{  
"\_index":"myindex",  
"\_score":100,  
"\_source": {  
"myfield1": "i have a twin",  
"name":"fred"  
}  
},

{  
"\_index":"myindex",  
"\_score":50,  
"\_source": {  
"myfield1": "i have a twin",  
"name":"george"  
}  
},

{  
"\_index":"myindex",  
"\_score":10,  
"\_source": {  
"myfield1": "i DONT have a twin",  
"name":"doug"  
}  
}

I would like to incorporate this agg in my query here...

{  
"query": {  
"function\_score": {  
"query": {  
"bool": {  
"should": [  
{  
"match\_phrase\_prefix": {  
"myfield": {  
"query": "i have",  
"fuzziness": 1,  
"slop":2,  
"max\_expansions": 10,  
"prefix\_length": 1  
}  
}  
}  
]  
}  
},  
"boost\_mode": "replace",  
"functions": [  
{  
"script\_score": {  
"script": "\_score \* ..."  
}  
}  
]  
}  
}  
}

--  
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/0d364cae-d823-4dc2-ae34-306fa37a7455%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0d364cae-d823-4dc2-ae34-306fa37a7455%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![chimpsarehungry](https://avatars.discourse-cdn.com/v4/letter/c/ba8739/32.png) [@chimpsarehungry](https://discuss.elastic.co/u/chimpsarehungry)\
**Post date:** [April 22, 2014, 6:53pm UTC](https://discuss.elastic.co/t/how-to-use-aggregations/17132/2 "2014-04-22T18:53:51Z")

</div>

I was hoping this would have the right effect ... but it didn't. Am I close?

{  
"query": {  
"function\_score": {  
"query": {  
"bool": {  
"should": [  
{  
"match\_phrase\_prefix": {  
"myfield1": {  
"query": "i have",  
"fuzziness": 1,  
"slop": 2,  
"max\_expansions": 10,  
"prefix\_length": 1  
}  
}  
}  
]  
}  
},  
"boost\_mode": "replace",  
"functions": [  
{  
"script\_score": {  
"script": "\_score \* [...] "  
}  
}  
]  
}  
},  
"aggs": {  
"myfield1": {  
"terms": {  
"field": "myfield1",  
"size": 2,  
"order": {  
"max\_score": "desc"  
}  
},  
"aggs": {  
"max\_score": {  
"max": {  
"field": "\_doc.score"  
}  
}  
}  
}  
}  
}

--  
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/9613fb75-626a-445c-a92b-1e4fbeef84be%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9613fb75-626a-445c-a92b-1e4fbeef84be%40googlegroups.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:34am UTC](https://discuss.elastic.co/t/how-to-use-aggregations/17132/3 "2017-07-06T01:34:16Z")

</div>


