In the previous two parts (Part1 and Part2), we learned how to configure and query SOLR to obtain the auto-complete function. Today, let's take a look at how to add fields to suggester to provide automatic completion.
Component Configuration
Add the following parameters to the Configuration component of the previous phase:
<str name="sourceLocation">dict.txt</str>
In this way, our configuration becomes:
<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>
In this example, suggestgroup uses the file named dict.txt as the SOLR configuration dictionary.
HandlerConfiguration
The handler configuration also requires an additional parameter:
<str name="spellcheck.onlyMorePopular">true</str>
Complete Configuration:
<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 tells SOLR that when the number of query results exceeds the set count, more clicks are returned.
Dictionary
We told SOLR to use this field. What is the length of this field? Here is an example:
# sample dict
Hard disk hitachi
Hard disk wd 2.0
Hard disk jjdd 3.0
What is the result of this dictionary? Each phrase is placed in a separate line, and each line ends with the weight of the modified phrase (the weight and the phrase are separated by TAB characters ).Spellcheck. onlymorepopular = trueThe default value of the parameter in Hong Kong is 1.0. This field must be stored in the encoding format of the UTF-8. # Characters before each line will be ignored (comment rows ).
Data
In this way, we do not need data. fields are data.
Run
After building suggester again, let's take a look at its running status and enter the following command:
/suggest?q=Har
The result is as follows:
<?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 wd</str>
<str>Hard disk hitachi</str>
</arr>
</lst>
</lst>
</lst>
</response>
Conclusion
As expected, suggest results are sorted by weight. The case sensitivity here (note the first letter ).
What suggestions do you have? If we have a good dictionary, the weight of this dictionary is generated based on the user's query behavior, then the user will certainly like it! If you do not have a good dictionary, do not use this method.
Next step
In the next issue, let's take a look at the index structure and size produced by suggest in different ways.
Original URL: http://java.dzone.com/news/solr-and-autocomplete-part-3? MZ = 33057-solr_lucene