# Limiting top score results returned based on conditional (same value of a field)

**URL:** <https://discuss.elastic.co/t/limiting-top-score-results-returned-based-on-conditional-same-value-of-a-field/17114>\
**Category:** Elasticsearch\
**Created:** [April 21, 2014, 8:16pm UTC](https://discuss.elastic.co/t/limiting-top-score-results-returned-based-on-conditional-same-value-of-a-field/17114 "2014-04-21T20:16:17Z")\
**Posts on this page:** 2\
**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 21, 2014, 8:16pm UTC](https://discuss.elastic.co/t/limiting-top-score-results-returned-based-on-conditional-same-value-of-a-field/17114/1 "2014-04-21T20:16:17Z")

</div>

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

the hits before this filter 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 (filter?) 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/69c0419c-5a2f-4f3a-9a33-2c75353e81f7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/69c0419c-5a2f-4f3a-9a33-2c75353e81f7%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/limiting-top-score-results-returned-based-on-conditional-same-value-of-a-field/17114/2 "2017-07-06T01:34:29Z")

</div>


