ElasticSearch query for most relevant documents

I have these documents
{
"_id" : "1",
"name" : "Nopales",
}
{
"_id" : "2",
"name" : "ginger ale",
}
{
"_id" : "3",
"name" : "Kale",
}
{
"_id" : "4",
"name" : "Triticale Flour Whole Grain",
}

and the request is to find the matching words with the word "ale" is,
GET /_search
{
"query": {
"query_string" : {
"default_field" : "name", "query" : "(ale) OR (ale) OR(ale)OR(ale)"
}
}
}

In ElasticSearch, I tried to makeit search for a part of a word. My example data is

{
"_id" : "1",
"name" : "Nopales",
}
{
"_id" : "2",
"name" : "ginger ale",
}
{
"_id" : "3",
"name" : "Kale",
}
{
"_id" : "4",
"name" : "Triticale Flour Whole Grain",
}

and the request is to find the matching words with the word "ale is"

GET /_search
{
"query": {
"query_string" : {"default_field" : "name", "query" : "ale"}
}

}

and it returns the results like

Kale, Triticale Flour Whole Grain, ginger ale, Nopales

but the best match is ginger ale, which contains the exact word, then kale, then nopales based on word counts then Triticale Flour Whole Grain. Anyone have idea how to achieve this?

You can combine 2 queries within a should clause of a bool query:

  • Full term match
  • Partial term match

If needed, boost the full term one.

Full example here

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