Solr and Autocomplete (part 3)
Join the DZone community and get the full member experience.Join For Free
in the previous parts ( part 1 , part. 2 ) of the cycle, we learned how to configure and query solr to get the autocomplete functionality. in today’s entry i will show you how to add the dictionary to the suggester, and thus have an impact on the generated suggestions.
to configure the component presented in the previous part of the cycle add the following parameter:
thus our configuration should look like this:
<searchcomponent name="suggest" class="solr.spellcheckcomponent"> <lst name="spellchecker"> <str name="name">suggest</str> <str name="classname">org.apache.solr.spelling.suggest.suggester</str> <str name="lookupimpl">org.apache.solr.spelling.suggest.tst.tstlookup</str> <str name="field">name_autocomplete</str> <str name="sourcelocation">dict.txt</str> </lst> </searchcomponent>
with the parameter we informed the component to use the dictionary named dict.txt which should be placed in the solr configuration directory.
the handler configuration also gets one additional parameter which is:
so our configuration should be as follows:
<requesthandler name="/suggest" class="org.apache.solr.handler.component.searchcomponent"> <lst name="defaults"> <str name="spellcheck">true</str> <str name="spellcheck.dictionary">suggest</str> <str name="spellcheck.count">10</str> <str name="spellcheck.onlymorepopular">true</str> </lst> <arr name="components"> <str>suggest</str> </arr> </requesthandler>
this parameter tell solr, to return only those suggestions for which the number of results is greater than the number of results for the current query.
we told solr to use the dictionary, but how should this dictionary look like ? for the purpose of this post i defined the following dictionary:
# sample dict hard disk hitachi hard disk wd 2.0 hard disk jjdd 3.0
what is the construction of a dictionary? each of the phrases (or single words) is located in a separate line. each line ends with the weight of the phrase (between the weight and the phrase is a tab character) which is used together with the parameter spellcheck.onlymorepopular=true (the higher the weight, the higher the suggestion will be). the default weight value is 1.0. a dictionary should be saved in utf-8 encoding. lines beginning with # character are skipped.
in this case we don’t need data – we will only use the defined dictionary.
let’s check how it works
to check how our mechanism behaves i sent the following query to solr, of course after rebuilding of the suggester index:
as a result we get the following:
<?xml version="1.0" encoding="utf-8"?> <response> <lst name="responseheader"> <int name="status">0</int> <int name="qtime">0</int> </lst> <lst name="spellcheck"> <lst name="suggestions"> <lst name="dys"> <int name="numfound">3</int> <int name="startoffset">0</int> <int name="endoffset">3</int> <arr name="suggestion"> <str>hard disk jjdd</str> <str>hard disk hitachi</str> <str>hard disk wd</str> </arr> </lst> </lst> </lst> </response>
a few words at the end
as you can see the suggestions are sorted by on the basis of weight, as expected. it is worth noting that the query was passed with a capital letter, which is also important – the lowercased query will return empty suggestion list.
what can you say about the method – if we have a very good dictionaries generated on the basis of weights such as customer behavior this is the method for you and your customers will love it. i would not recommend it if you don’t have good dictionaries – there is a very high chance that your suggestions will be of poor quality.
what will be next ?
the number of tasks this week didn’t let me finish the performance tests and that’s why, in the next part of the cycle, i’ll try to show you how each method behaves with various index structure and size.
Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.