Elasticsearch query for "starts with "searching on whole string

(Sharat Jagannath) #1

i have documents with a field called title having data like "the lord of the rings","lord of the rings","the ring",etc
I would like to do a search as you type feature.
So if user types "th", the order of the results should be -
"the lord of the ring",
"the ring",
"lord of the rings"
since i want the strings that start with "th" to appear first and alphabetically.
i tried looking into edgengrams, but that does it for every word in the string.
I would like to do it only from beginning of string.

Can you please let me know what are the analyzers i need to use to achieve this?


(Zachary Tong) #2

The two usual approaches for this are:

  • Use edge n-grams, but with the keyword tokenizer. As you noted, if you use ngrams with a default tokenizer (standard, etc), you'll get ngrams for each word independently. The keyword tokenizer only emits a single token however; the original string. So keyword + edge-ngram will give you what you want.

  • Use match_phrase_prefix. If you search for "the lo", that query will essentially put a wildcard at the end so it becomes "the lo*". It maintains phrase matching semantics, so it will only match "the lord", "the look", etc.

(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.