全文檢索搜索引擎都會有這樣一個功能:輸入一個字符便自動提示出可選的短語:
html
要實現這種功能,能夠利用solr的SuggestComponent,SuggestComponent這種方法利用Lucene的Suggester實現,並支持Lucene中可用的全部查找實現。算法
這個是本身的字段:apache
<field name="name" type="string" indexed="true" stored="true"/> <field name="username" type="string" indexed="true" stored="true"/> <field name="password" type="string" indexed="true" stored="true"/> <field name="phone" type="string" indexed="true" stored="true"/>
<field name="suggest_username" type="text_suggest" indexed="true" stored="true"/> <copyField source="username" dest="suggest_username"/>
copyField的source表示源,dest表示目標。瀏覽器
<fieldType name="text_suggest" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.LowerCaseFilterFactory"/> <filter class="solr.EnglishPossessiveFilterFactory"/> </analyzer> </fieldType>
tokenizer就是分詞器,官方解釋:ide
The job of a tokenizer is to break up a stream of text into tokens, where each token is (usually) a sub-sequence of the characters in the text。測試
就是指將文本流分解爲標記tokens,這寫tokens也是文本的子序列。優化
filter就是過濾器,官方解釋:ui
The job of a filter is usually easier than that of a tokenizer since in most cases a filter looks at each token in the stream sequentially and decides whether to pass it along, replace it or discard it.搜索引擎
不一樣的過濾器將輸入流替換或者丟棄或者直接經過。code
solrconfig.xml文件也在新建核心core的conf文件夾下
<searchComponent name="suggest" class="solr.SuggestComponent"> <lst name="suggester"> <str name="name">AnalyzingSuggester</str> <str name="lookupImpl">AnalyzingLookupFactory</str> <str name="dictionaryImpl">DocumentDictionaryFactory</str> <str name="field">suggest_username</str> <str name="weightField">suggest_username</str> <str name="payloadField">id</str> <str name="suggestAnalyzerFieldType">text_suggest</str> <str name="buildOnStartup">false</str> </lst> </searchComponent>
在searchComponent中的suggester須要配置一些參數。
buildOnStartup;啓動的時候構建建議索引。
<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy" > <lst name="defaults"> <str name="suggest">true</str> <str name="suggest.count">10</str> </lst> <arr name="components"> <!-- 上面配置的searchComponent名字suggest --> <str>suggest</str> </arr> </requestHandler>
這裏
經過瀏覽器地址欄輸入鏈接測試:
http://127.0.0.1:8983/solr/user/suggest?suggest=true&suggest.build=true&suggest.dictionary=AnalyzingSuggester&suggest.q=a
suggest.q=a ;suggest查詢內容。
https://lucene.apache.org/solr/guide/8_1/suggester.html#suggest-request-handler-parameters
"responseHeader": { "status": 0, "QTime": 10 }, "command": "build", "suggest": { "AnalyzingSuggester": { "aoa": { "numFound": 3, "suggestions": [ { "term": "aoa lee", "weight": 0, "payload": "7859b42e-3428-40c0-9036-6d50767a5ff2" }, { "term": "aoa lee key", "weight": 0, "payload": "0bead5d5-2570-44ba-830b-030f8a888ea3" }, { "term": "aoa lee key lol bob", "weight": 0, "payload": "9cc3c4d7-7d34-422b-8164-a4c4c92caa08" } ] } } } }
返回的結果中主要有三個參數: