How can I boost the score for documents in which my query matches a particular field earlier. For example, searching for "super man" should give "super man returns" a higher score than "there is my super man". Is this possible?
9
votes
Uh, store the first few words explicitly in another field, and boost matches on this field.
– Jesvin Jose
The problem there is that the size of the query can vary from say 3 characters to say 100 characters, and so determining how many words/chars to index separately can be difficult.
– techfoobar
Secondly, suppose i index the first 25 characters, and one record has "my super man blah.." and another record has "super man returns blah.." - both will match the query "super man" and both will be boosted when i boost this secondary field.
– techfoobar
2 Answers
2
votes
2
votes
Solved it myself after reading a LOT about this online. What specifically helped me was a reply on nabble which goes like (I used dismax, so explaining that here):
- Create a separate field named say 'nameString' which stores the value as
"_START_ <actual data>"
- Change the search query to
"_START_ <actual query>"
- Add the new field
nameString
as one of the fields to look in in the query fields param (qf) - While searching use the parameter pf (phrase field) as the new field
nameString
with a phrase slop of 1 or 2 (lower values would mean stricter searching)
Your final query params will be something like:
q=_START_ <actual query>
defType=dismax
qf=name nameString /* look in name field as well as nameString field */
pf=nameString /* phrase field in nameString */
ps=2 /* phrase slop */