二、Solr配置中文分詞器IKAnalyzer並配置業務域
一、solr域的介紹
在solr中域的概念與lucene中域的概念相同,資料庫的一條記錄或者一個檔案的資訊就是一個document,資料庫記錄的欄位或者檔案的某個屬性就是一個Field域,solr中對索引的檢索也是對Field的操作。lucene中對域的操作是通過程式碼,solr對域的管理是通過一個配置檔案schema.xml。
solr中域的型別是schema.xml中<fieldType>元素常用的field型別
<!--string 型別 在儲存索引時不進行分詞 sortMissingLast:設定為true時 沒有該filed的資料將排在有該Field的資料後面,忽略請求時的排序規則,預設為false。--> <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> <!-- boolean 型別只有兩個值 true false--> <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/> <!--用於直接數值搜尋,該型別不分詞 --> <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/> <!--用於數值範圍搜尋,進行分詞 通過設定precisionStep的值可以提高檢索速度,8是solr的推薦值 --> <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" positionIncrementGap="0"/> <fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" positionIncrementGap="0"/> <fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" positionIncrementGap="0"/> <fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/> <!--日期型別--> <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="tdate" class="solr.TrieDateField" precisionStep="6" positionIncrementGap="0"/> <!--二進位制型別--> <fieldtype name="binary" class="solr.BinaryField"/> <!--隨機數型別--> <fieldType name="random" class="solr.RandomSortField" indexed="true" /> <!-- text_general 型別 進行分詞 --> <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100"> <!--建立索引時的配置 --> <analyzer type="index"> <!-- tokenizer 建立索引使用的分詞器 --> <tokenizer class="solr.StandardTokenizerFactory"/> <!--filter 分詞時的過濾器 class="solr.StopFilterFactory" 處理停用詞 words:配置停用詞--> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <!-- filter 分詞時的過濾器 class="solr.LowerCaseFilterFactory" 處理大小寫轉換問題(將大寫轉小寫)--> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <!--查詢索引時的配置 --> <analyzer type="query"> <!-- tokenizer 對查詢條件分詞時使用的分詞器 --> <tokenizer class="solr.StandardTokenizerFactory"/> <!--filter 分詞時的過濾器 class="solr.StopFilterFactory" 處理停用詞 words:配置停用詞--> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <!--filter 分詞時的過濾器 class="solr.SynonymFilterFactory" 處理同義詞 synonyms:配置同義詞--> <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> <!-- filter 分詞時的過濾器 class="solr.LowerCaseFilterFactory" 處理大小寫轉換問題(將大寫轉小寫)--> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType>
solr在操作Field域時需要在schema.xml中定義(根據自己的業務需求自定義)。
<!--name域的名稱 type:域的型別 indexed:是否使用該域搜尋 stored:是否儲存 如果不儲存在查詢時是查不到該域的 但可以進行搜尋 multiValued:是否支援儲存多值 --> <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/> <field name="subject" type="text_general" indexed="true" stored="true"/> <field name="description" type="text_general" indexed="true" stored="true"/> <field name="comments" type="text_general" indexed="true" stored="true"/> <field name="author" type="text_general" indexed="true" stored="true"/> <field name="keywords" type="text_general" indexed="true" stored="true"/> <field name="category" type="text_general" indexed="true" stored="true"/> <field name="resourcename" type="text_general" indexed="true" stored="true"/> <field name="url" type="text_general" indexed="true" stored="true"/> <field name="content_type" type="string" indexed="true" stored="true" multiValued="true"/> <field name="last_modified" type="date" indexed="true" stored="true"/> <field name="links" type="string" indexed="true" stored="true" multiValued="true"/>
1、唯一域
<!-- id 域 也叫唯一域 每一個文件必須有唯一域 -->
<uniqueKey>id</uniqueKey>
2、動態域
<!-- 動態域 *_i:萬用字元 --> <dynamicField name="*_i" type="int" indexed="true" stored="true"/> <dynamicField name="*_is" type="int" indexed="true" stored="true" multiValued="true"/> <dynamicField name="*_s" type="string" indexed="true" stored="true" /> <dynamicField name="*_ss" type="string" indexed="true" stored="true" multiValued="true"/>
3、複製域 copyField 可以將多個Field複製到一個Field中,一便進行統一檢索。
<copyField source="title" dest="text"/>
例如:搜尋title標題、description內容 、author作者,我們可以定義title、description、author的複製域
a、先建立域
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="author" type="text_general" indexed="true" stored="true"/>
<field name="description" type="text_general" indexed="true" stored="true"/>
<field name="keywords" type="text_general" indexed="true" stored="false"/>
b、建立copyField 域
<!--source:源域 dest:目標域 -->
<copyField source="title" dest="keywords"/>
<copyField source="author" dest="keywords"/>
<copyField source="description" dest="keywords"/>
c、配置完成後匯入索引。
二、配置中文分析器
在solr中預設是中文分析器,需要手工配置。配置一個FieldType,在FieldType中指定中文分析器。
1、使用 IK-Analyzer中文分析器 將該分析器檔案上傳伺服器
2、將需要把分析器的jar包(IKAnalyzer2012FF_u1.jar)新增到solr工程中。
[[email protected] IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
3、把IKAnalyzer需要的擴充套件詞典及停用詞詞典、配置檔案複製到solr工程的classpath。
(1) 在usr/local/solr/tomcat/webapps/solr/WEB-INF/目錄下建立classes目錄 [[email protected] WEB-INF]# mkdir classes
(2)複製檔案 [[email protected] IK Analyzer 2012FF_hf1]# cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
ext_stopword.dic:擴充套件詞詞典
mydict.dic:停用詞詞典
注意:擴充套件詞典及停用詞詞典的字符集必須是utf-8。不能使用windows記事本編輯。
4、配置fieldType。需要在/usr/local/solr/solrhome/collection1/conf/schema.xml中配置。技巧:使用vi、vim跳轉到文件開頭gg。跳轉到文件末尾:G
在檔案末尾新增fieldType
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
name="text_ik":一個標識可以隨便寫
class="solr.TextField":分詞分析器
二、配置業務欄位
Solr中的欄位必須是先定義後使用。該配置要與我們的實際業務關聯。
業務欄位判斷標準:
1、在搜尋時是否需要在此欄位上進行搜尋。例如:商品名稱、商品的賣點、商品的描述
2、後續的業務是否需要用到此欄位。例如:商品id。
本人這次專案需要用到的欄位:
1、商品id
2、商品title
3、賣點sell_point
4、價格price
5、商品圖片image
6、商品分類名稱category_name
7、商品描述item_des
在solrhome/collection1/conf/schema.xml 中新增 Solr中的業務欄位:
id——商品id
其他的對應欄位建立solr的欄位。
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="long" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category_name" type="string" indexed="true" stored="true" />
<field name="item_desc" type="text_ik" indexed="true" stored="false" />
<!-- 建立複製域 將其他域上的搜尋關鍵詞都複製到一個域上 是solr對搜所的優化-->
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>
<copyField source="item_desc" dest="item_keywords"/>
將資料寫入索引庫的欄位要與該配置相同。
重啟tomcat