# Combining an 'exact match' with other matches

**URL:** <https://discuss.elastic.co/t/combining-an-exact-match-with-other-matches/8557>\
**Category:** Elasticsearch\
**Created:** [July 30, 2012, 12:51am UTC](https://discuss.elastic.co/t/combining-an-exact-match-with-other-matches/8557 "2012-07-30T00:51:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_3](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Matt\_3](https://discuss.elastic.co/u/Matt_3)\
**Post date:** [July 30, 2012, 12:51am UTC](https://discuss.elastic.co/t/combining-an-exact-match-with-other-matches/8557/1 "2012-07-30T00:51:14Z")

</div>

I have a search for a string against some 'names'. I want to prioritize an  
exact match as the first result returned and all other matches, or hits, to  
following.

Could I or should I have a request which combines a 'must' with a 'should'?

If I search for 'John' and got a heap of records back but one happened to  
be 'John' with no last name, not 'John Brown' or 'John Smith' or 'Johnny',  
an exact string match, I want to get that result first.

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [July 30, 2012, 10:34am UTC](https://discuss.elastic.co/t/combining-an-exact-match-with-other-matches/8557/2 "2012-07-30T10:34:12Z")

</div>

Hi Matt,

What your could is that you store two versions of a field by using a  
multi field type:

> **[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.

One field would be analysed and one wouldn't be analysed. The not  
analysed field will be used for exact matches.  
You then define a boolean query with two should clauses. One clause  
holds a query for  
the analysed field. The other clause holds a query for the not  
analysed field, but  
is wrapped in a custom\_boost\_factor query:

> **[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.

This query allows you to impact the score hits in the not analysed  
field higher by specifying a boost factor.

The scoring formula gives hits in shorter fields a higher positive  
boost than for hits in longer fields.  
The length of a field is measured in number of terms. This already  
might result in the behavior you want.  
This scoring behavior is in place as long as omit\_norms is set to  
false on string field types, which is the default.

Martijn

On 30 July 2012 02:51, Matt [szermierz@gmail.com](mailto:szermierz@gmail.com) wrote:

> I have a search for a string against some 'names'. I want to prioritize an  
> exact match as the first result returned and all other matches, or hits, to  
> following.
> 
> Could I or should I have a request which combines a 'must' with a 'should'?
> 
> If I search for 'John' and got a heap of records back but one happened to be  
> 'John' with no last name, not 'John Brown' or 'John Smith' or 'Johnny', an  
> exact string match, I want to get that result first.

---

<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, 3:18am UTC](https://discuss.elastic.co/t/combining-an-exact-match-with-other-matches/8557/3 "2017-07-06T03:18:30Z")

</div>


