I have a field "description" and I am searching a text against this filed having default Similarity scoring. Say, i have searched for "hello man" and i am getting results like "hello mango", "hello man how you", "hello man10", "hello man", "hello mann". All these are expected results, but i want to display the exact matches (here it is "hello man") at the very top of partial matches. To get results against typo i am using nGramFilterFactory while indexing and querying and it is the requirement. Only thing i am worried here is the exact match is not coming at the top.
Please suggest how we can do this or what kind of approach i need to go for. Please help.
You should define another field in your schema.xml
that doesn't do any analysis on your data. The easiest way to do this in your case is probably doing the following:
<field name="exact_description" type="string" indexed="true" stored="false" multiValued="true" />
<copyField source="originalColumnName" dest="exact_description" docValues="true" />
Using the string
type will keep Solr from tokenizing or doing anything else to your data.
Then, when building your query, you can put something like the following before the rest of your query:
exact_description:"hello man"^100.0
Make sure you put a boost (the ^100.0
) of your choice on exact_description
, so exact matches will be forced to the top of your results.
When you create your new field, make sure you base it off of a field that hasn't had any analysis performed on it. For example, in my schema, I have a field called exact_match
, which is copied from the following:
<field name="match" type="string" indexed="false" stored="true" required="false" omitNorms="true" />
Now, I could have just used match
for exact matches in search since match
is just a string, but for spec reasons I had to create exact_match
like this:
<field name="exact_match" type="string" indexed="true" stored="false" multiValued="true" />
<copyField source="match" dest="exact_match" docValues="true" />