您好,登錄后才能下訂單哦!
本篇內容主要講解“solr4.7拼寫檢查怎么實現”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“solr4.7拼寫檢查怎么實現”吧!
①拼寫檢查不同于其他域,它在建立索引時需要分詞,但是檢索時不需要分詞,所以要建立一個特殊的域,以供拼寫檢查:
在schema.xml文件里設置所需的拼寫檢查域都有哪些字段:
<field name="spell" type="text_spell" indexed="true" stored="false" multiValued="true" /> <copyField source="name" dest="spell"/> <copyField source="content" dest="spell"/> <fieldType name="text_spell" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> </analyzer> <analyzer type="query"> <tokenizer class="solr.WhitespaceTokenizerFactory"/> </analyzer> </fieldType>
②在solrconfig.xml文件里設置:
<searchComponent name="spellerror" class="solr.SpellCheckComponent"> <str name="queryAnalyzerFieldType">text_spell</str> <!-- a spellchecker built from a field of the main index --> <lst name="spellchecker"> <str name="name">default</str> <str name="field">spell</str> <str name="classname">solr.DirectSolrSpellChecker</str> <str name="distanceMeasure">internal</str> <float name="accuracy">0.5</float> <int name="maxEdits">2</int> <int name="minPrefix">1</int> <int name="maxInspections">5</int> <int name="minQueryLength">2</int> <float name="maxQueryFrequency">0.01</float> </lst> <lst name="spellchecker"> <str name="classname">solr.FileBasedSpellChecker</str> <str name="name">file</str> <str name="sourceLocation">spellings.txt</str> <str name="characterEncoding">UTF-8</str> <str name="spellcheckIndexDir">spellcheckerFile</str> </lst> </searchComponent> <requestHandler name="/spell" class="solr.SearchHandler" startup="lazy"> <lst name="defaults"> <str name="df">spell</str> <str name="spellcheck.dictionary">default</str> <str name="spellcheck">on</str> <!-- <str name="spellcheck.extendedResults">true</str> <str name="spellcheck.count">10</str> <str name="spellcheck.alternativeTermCount">5</str> <str name="spellcheck.maxResultsForSuggest">5</str> --> <str name="spellcheck.collate">true</str> <str name="spellcheck.collateExtendedResults">true</str> <!-- <str name="spellcheck.maxCollationTries">10</str> <str name="spellcheck.maxCollations">5</str> --> </lst> <arr name="last-components"> <str>spellerror</str> </arr> </requestHandler>
③solrj里的代碼
/** * @method: testSpellCheck * @Description: 拼寫檢查 * * @author: ChenYW * @date 2014-4-15 下午06:14:56 */ public String spellCheck(String word){ SolrQuery query = new SolrQuery(); query.set("defType","edismax");//加權 query.set("qf","name^20.0"); query.set("spellcheck", "true"); query.set("spellcheck.q", word); query.set("qt", "/spell"); query.set("spellcheck.build", "true");//遇到新的檢查詞,會自動添加到索引里面 query.set("spellcheck.count", 5); try { QueryResponse rsp = server.query(query); SpellCheckResponse re=rsp.getSpellCheckResponse(); if (re != null) { if(!re.isCorrectlySpelled()){ String t = re.getFirstSuggestion(word);//獲取第一個推薦詞 System.out.println("推薦詞:" + t); return t; } } } catch (SolrServerException e) { e.printStackTrace(); } return null; }
到此,相信大家對“solr4.7拼寫檢查怎么實現”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。