Combining an 'exact match' with other matches


(Matt-3) #1

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.


(Martijn Van Groningen) #2

Hi Matt,

What your could is that you store two versions of a field by using a
multi field type:
http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html

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:
http://www.elasticsearch.org/guide/reference/query-dsl/custom-boost-factor-query.html

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


(system) #3