該文使用 Centos6.5 64 位 solr4.10.3 IK-Analyzer中文分析器
一、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中文分析器 將該分析器文件上傳服務器 /opt/tools/IK Analyzer 2012FF_hf1
2、將需要把分析器的jar包(IKAnalyzer2012FF_u1.jar)添加到solr工程中。
[root@localhost IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr4/tomcat7/webapps/solr/WEB-INF/lib/
3、把IKAnalyzer需要的擴展詞典及停用詞詞典、配置文件復制到solr工程的classpath。
(1) 在usr/local/solr4/tomcat7/webapps/solr/WEB-INF/目錄下創建classes目錄 [root@localhost WEB-INF]# mkdir classes
(2)復制文件 [root@localhost IK Analyzer 2012FF_hf1]# cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /usr/local/solr4/tomcat7/webapps/solr/WEB-INF/classes
ext_stopword.dic:擴展詞詞典
mydict.dic:停用詞詞典
注意:擴展詞典及停用詞詞典的字符集必須是utf-8。不能使用windows記事本編輯。
4、配置fieldType。需要在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

