# Sort multi\_match query by exact matches first

**URL:** <https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468>\
**Category:** Elasticsearch\
**Created:** [January 29, 2014, 10:06am UTC](https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468 "2014-01-29T10:06:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![morninlark](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@morninlark](https://discuss.elastic.co/u/morninlark)\
**Post date:** [January 29, 2014, 10:06am UTC](https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468/1 "2014-01-29T10:06:01Z")

</div>

Hi there,

I am executing the following query:  
{  
"from":0,  
"size":5,  
"explain": "true",  
"query":{  
"bool":{  
"must":[  
{  
"multi\_match":{  
"query":"Marco",  
"fields":["FirstName","MiddleName","LastName"],  
"fuzziness":0.6  
}  
}  
]  
}  
}  
}

As the results are by default sorted by the score, I thought that would give me the most matching results first. But the first result is a Marco, then two Mario and then another Marco:

hits: [  
{  
\_shard: 8  
\_node: pFk-akByRACHUsGiyVa5UQ  
\_index: my\_index  
\_type: my\_type  
\_id: 52  
\_score: 3.0794415  
\_source: {  
LastName: Orozco Arriola  
MiddleName: Antonio  
FirstName: Marco  
},  
\_explanation: {  
value: 3.0794415  
description: max of:  
details: [  
{  
value: 3.0794415  
description: weight(FirstName:marco in 4) [PerFieldSimilarity], result of:  
details: [  
{  
value: 3.0794415  
description: fieldWeight in 4, product of:  
details: [  
{  
value: 1  
description: tf(freq=1.0), with freq of:  
details: [  
{  
value: 1  
description: termFreq=1.0  
}  
]  
}  
{  
value: 3.0794415  
description: idf(docFreq=1, maxDocs=16)  
}  
{  
value: 1  
description: fieldNorm(doc=4)  
}  
]  
}  
]  
}  
]  
}  
},  
{  
\_shard: 9  
\_node: pFk-akByRACHUsGiyVa5UQ  
\_index: my\_index  
\_type: my\_type  
\_id: 114  
\_score: 2.9459102  
\_source: {  
LastName: Barrios Falla  
MiddleName: Mario  
FirstName: Jorge  
},  
\_explanation: {  
value: 2.9459102  
description: max of:  
details: [  
{  
value: 2.9459102  
description: weight(MiddleName:mario^0.8 in 8) [PerFieldSimilarity], result of:  
details: [  
{  
value: 2.9459102  
description: fieldWeight in 8, product of:  
details: [  
{  
value: 1  
description: tf(freq=1.0), with freq of:  
details: [  
{  
value: 1  
description: termFreq=1.0  
}  
]  
}  
{  
value: 2.9459102  
description: idf(docFreq=1, maxDocs=14)  
}  
{  
value: 1  
description: fieldNorm(doc=8)  
}  
]  
}  
]  
}  
]  
}  
},  
{  
\_shard: 12  
\_node: pFk-akByRACHUsGiyVa5UQ  
\_index: my\_index  
\_type: my\_type  
\_id: 94  
\_score: 2.7917595  
\_source: {  
LastName: Rivera Cabrera  
MiddleName: Israel  
FirstName: Mario  
},  
\_explanation: {  
value: 2.7917595  
description: max of:  
details: [  
{  
value: 2.7917595  
description: weight(FirstName:mario^0.8 in 6) [PerFieldSimilarity], result of:  
details: [  
{  
value: 2.7917595  
description: fieldWeight in 6, product of:  
details: [  
{  
value: 1  
description: tf(freq=1.0), with freq of:  
details: [  
{  
value: 1  
description: termFreq=1.0  
}  
]  
}  
{  
value: 2.7917595  
description: idf(docFreq=1, maxDocs=12)  
}  
{  
value: 1  
description: fieldNorm(doc=6)  
}  
]  
}  
]  
}  
]  
},  
{  
\_shard: 3  
\_node: pFk-akByRACHUsGiyVa5UQ  
\_index: my\_index  
\_type: my\_type  
\_id: 44  
\_score: 2.7047482  
\_source: {  
LastName: Lemus Salguero  
MiddleName: Antonio  
FirstName: Marco  
},  
\_explanation: {  
value: 2.7047482  
description: max of:  
details: [  
{  
value: 2.7047482  
description: weight(FirstName:marco in 1) [PerFieldSimilarity], result of:  
details: [  
{  
value: 2.7047482  
description: fieldWeight in 1, product of:  
details: [  
{  
value: 1  
description: tf(freq=1.0), with freq of:  
details: [  
{  
value: 1  
description: termFreq=1.0  
}  
]  
}  
{  
value: 2.7047482  
description: idf(docFreq=1, maxDocs=11)  
}  
{  
value: 1  
description: fieldNorm(doc=1)  
}  
]  
}  
]  
}  
]  
}  
}  
]

Also with the explain function I don't really see what I have to change to get

- first all exact matches ("Marco")
- then the fuzzier ones ("Mario")  
Can you help me there?

Cheers!

---

<div class="post-metadata">

**Author:** ![Binh\_Ly](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@Binh\_Ly](https://discuss.elastic.co/u/Binh_Ly)\
**Post date:** [January 29, 2014, 1:48pm UTC](https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468/2 "2014-01-29T13:48:41Z")

</div>

Try something like this and see if it helps:

{  
"query": {  
"bool": {  
"should": [  
{  
"multi\_match": {  
"query": "Marco",  
"fields": [  
"f",  
"m",  
"l"  
]  
}  
},  
{  
"multi\_match": {  
"query": "Marco",  
"fields": [  
"f",  
"m",  
"l"  
],  
"fuzziness": 0.6  
}  
}  
]  
}  
}  
}

--  
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/96fb3334-6d5c-48a0-9f51-057876ef5566%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/96fb3334-6d5c-48a0-9f51-057876ef5566%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![morninlark](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@morninlark](https://discuss.elastic.co/u/morninlark)\
**Post date:** [January 29, 2014, 1:58pm UTC](https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468/3 "2014-01-29T13:58:57Z")

</div>

That works, thank you!!!

---

<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:54am UTC](https://discuss.elastic.co/t/sort-multi-match-query-by-exact-matches-first/15468/4 "2017-07-06T01:54:11Z")

</div>


