Higher score for documents which start with searched word

Hello,

I cannot figure out a way to assign higher relevance for the documents based on the order of the searched term in the document field.

For instance, if we search for "egg" in documents: "bagel, egg", "egg, raw, fresh", it should give higher score to the second one, because the word "egg" is first in this document, rather than second as in the other document.

Any ideas are appreciated

Hi - you can use multiple clauses in a bool query's should array. One clause is a basic position-independent match query and one which expects a match at the beginning [1]. Docs that match both clauses would score highest.

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html

2 Likes

Thanks a lot, Mark! I think this solves my problem.