pech0rin
pech0rin

Reputation: 5016

Elasticsearch - Query original field

I am creating a typeahead search with ES. I want to have partial matches to I have an ngram analyzer. Additionally, I want things that match at the beginning of a field to come up higher, so I have a prefix_phrase query which is boosted.

My problem is that I need to query the original value in the prefix_phrase or it will match all of the matched ngrams, not giving me what I want.

Example:

emails: [[email protected], [email protected]]
search: 123

I want 123_something to come back first. But because of the ngram analyzer the value [email protected] is also matching the prefix query.

Is there a way to query the original value of an analyzed field (aka the value that is actually stored in the document)

Something like this

{
   match: {
       query: 123
       type: 'prefix'
       fields: ["email"]
       original: 1
   }
}

Or do I need to just store the original value in a separate field?

This answer: search a analyzed field through the stored original value in elasticsearch points to documentation that doesn't exist. Looks like multifield values were deprecated.

Upvotes: 0

Views: 132

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

The best approach in this case is multi-fields, indeed, and the most recent documentation has been a bit restructured. You'll find out more about multi-fields here.

It is often useful to index the same field in different ways for different purposes. For instance, a string field could be indexed as an analyzed field for full-text search, and as a not_analyzed field for sorting or aggregations. Alternatively, you could index a string field with the standard analyzer, the english analyzer, and the french analyzer.

This is the purpose of multi-fields. Most datatypes support multi-fields via the fields parameter.

And a full example you can find here.

Upvotes: 1

Related Questions