solr IK中文分詞器安裝

講過solr在windows和linux下的安裝了,但是并沒有安裝中文分析器,這里補充吧。
安裝中文分詞器需要用到solrhome\collection1\conf下的schema.xml文件。所以有必要先說一下這個xml文件。

打開schema.xml后主要看到以下的幾個標簽:
1、fieldType:域類型定義
如:

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.StandardTokenizerFactory"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

class:是solr自己封裝的類型,可以根據(jù)該類型來創(chuàng)建數(shù)據(jù)跟查詢。
analyzer表示分析器,由分詞器(包括索引分詞器跟查詢分詞器)跟過濾器組成。

2、field:業(yè)務域定義
在該標簽內(nèi)定義具體的業(yè)務域,注意,域的類型必須是在fieldType中定義過的name,否則添加索引的時候會報錯。如:

<field name="name" type="text_general" indexed="true" stored="true"/>
   <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>

indexed:是否被索引
stored:是否被存儲
multivalued:是否可存儲多個值
solr其實已經(jīng)幫我們定義好了很多的field,比如name,id,cat等,用不用的看就看情況了。

3、copyField:復制域
如;

<copyField source="title" dest="text"/>
<copyField source="name" dest="text"/>

這就表示會將定義好的title和name兩個業(yè)務域映射(復制)到text域上,那么進行關(guān)鍵字查詢text域中的信息的時候就相當域同時查了title和name兩個域中信息,而不用進行兩次查詢,從而進行了統(tǒng)一的檢索提高了效率。

其實可以想的簡單一點,F(xiàn)ieldType就好像是我們在Java中自定義類型。

Field:
Field就是一個字段,定義一個Field很簡單:

<field name="price" type="long" indexed="true" stored="true"/>
基本上屬性也和FieldType類似,他的屬性會覆蓋掉FieldType的同名屬性。

CopyField:

你可能想讓document的一些字段可以多次使用。solr 有一個字段復制機制,可以提交多個不同類型字段集中到一個字段。字段復制主要涉及兩個概念,source和destination,一個是要復制的字段,另一個是要復制到哪個字段,以下是個例子:

<copyField source="cat" dest="text" maxChars="30000" />

上例中,如果text字段有數(shù)據(jù)的話,cat字段的內(nèi)容將被添加到text字段中。maxChars 參數(shù),一個int類型參數(shù),用于限制復制的字符數(shù)。

source和destination都支持通配符。以下是一個將所有以 _t 結(jié)尾的字段全部復制到text字段中。

<copyField source="*_t" dest="text" maxChars="25000" />

其實說的簡單一點,比如現(xiàn)在你要查詢包涵"Java"的博客, 那么你肯定要查內(nèi)容,標題是否包含Java,但是solr不能像SQL那樣,where tittle like '%Java%' or content like '%Java%'. 這個時候copyField就派上用場了, 定義一個新字段,將title和content 復制到這個新字段,索引的時候,直接從這個新字段查詢,這樣就達到目地了。 這便是copyField的典型應用場景 。注意:如果dest由多個source構(gòu)成,就需要將其指定為multiValued。
在網(wǎng)上找了一個例子:

<schema name="eshequn.post.db_post.0" version="1.1"  
    xmlns:xi="http://www.w3.org/2001/XInclude">  
     <fields>  
        <!-- for title -->  
        <field name="t" type="text" indexed="true" stored="false" />  
        <!-- for abstract -->  
        <field name="a" type="text" indexed="true" stored="false" />  
        <!-- for title and abstract -->  
        <field name="ta" type="text" indexed="true" stored="false" multiValued="true"/>  
    </fields>  
    <copyField source="t" dest="ta" />  
    <copyField source="a" dest="ta" />  
</schema>  

1、Linux下安裝Ik中文分析器

windows就不說了會linux下的配置更會windos下的配置。

第一步:
先看一下ik分析器文件夾吧,網(wǎng)上能找到:

image

將IK Analyzer 2012FF_hf1上傳到linux(我的還是放在/usr目錄下的)
復制IKAnalyzer2012FF_u1.jar(這個jar的版本一定要對,否則可能和solr4.10.3不搭配)
/usr/local/solr/tomcat/webapps/solr/WEB-INF/lib(這個在介紹安裝配置solr的時候創(chuàng)建的)下。

[root@itheima32 usr]# cd IK\ Analyzer\ 2012FF_hf1
[root@itheima32 IK Analyzer 2012FF_hf1]# 
總用量 2004
drwxr-xr-x. 5 root root    4096 6月  29 2017 doc
-rw-r--r--. 1 root root     168 6月  29 2017 ext_stopword.dic
-rw-r--r--. 1 root root 1165908 6月  29 2017 IKAnalyzer2012FF_u1.jar
-rw-r--r--. 1 root root     419 6月  29 2017 IKAnalyzer.cfg.xml
-rw-r--r--. 1 root root  841268 6月  29 2017 IKAnalyzer中文分詞器V2012_FF使用手冊.pdf
-rw-r--r--. 1 root root   17778 6月  29 2017 LICENSE.txt
-rw-r--r--. 1 root root      34 6月  29 2017 mydict.dic
-rw-r--r--. 1 root root     278 6月  29 2017 NOTICE.txt
[root@itheima32 IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib
[root@itheima32 IK Analyzer 2012FF_hf1]# cd 

第二步:
在/usr/local/solr/tomcat/webapps/solr/WEB-INF下創(chuàng)建classes文件夾,將擴展詞典、配置文件放到solr工程的WEB-INF/classes目錄下

[root@itheima32 IK Analyzer 2012FF_hf1]# cd /usr/local/solr/tomcat/webapps/solr/WEB-INF
[root@itheima32 WEB-INF]# ll
總用量 16
drwxr-xr-x. 2 root root 4096 6月  29 19:36 lib
-rw-r--r--. 1 root root 1210 12月  1 2014 weblogic.xml
-rw-r--r--. 1 root root 7044 6月  29 16:43 web.xml

2、把擴展詞典、配置文件放到solr工程的WEB-INF/classes目錄下。
[root@itheima32 WEB-INF]# mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[root@itheima32 WEB-INF]# ll
總用量 20
drwxr-xr-x. 2 root root 4096 6月  29 19:38 classes
drwxr-xr-x. 2 root root 4096 6月  29 19:36 lib
-rw-r--r--. 1 root root 1210 12月  1 2014 weblogic.xml
-rw-r--r--. 1 root root 7044 6月  29 16:43 web.xml
[root@itheima32 WEB-INF]# cd /usr/IK\ Analyzer\ 2012FF_hf1
[root@itheima32 IK Analyzer 2012FF_hf1]# ll
總用量 2004
drwxr-xr-x. 5 root root    4096 6月  29 2017 doc
-rw-r--r--. 1 root root     168 6月  29 2017 ext_stopword.dic
-rw-r--r--. 1 root root 1165908 6月  29 2017 IKAnalyzer2012FF_u1.jar
-rw-r--r--. 1 root root     419 6月  29 2017 IKAnalyzer.cfg.xml
-rw-r--r--. 1 root root  841268 6月  29 2017 IKAnalyzer中文分詞器V2012_FF使用手冊.pdf
-rw-r--r--. 1 root root   17778 6月  29 2017 LICENSE.txt
-rw-r--r--. 1 root root      34 6月  29 2017 mydict.dic
-rw-r--r--. 1 root root     278 6月  29 2017 NOTICE.txt
[root@itheima32 IK Analyzer 2012FF_hf1]# cp ext_stopword.dic IKAnalyzer.cfg.xml mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
[root@itheima32 IK Analyzer 2012FF_hf1]# ll  /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes
總用量 12
-rw-r--r--. 1 root root 168 6月  29 19:40 ext_stopword.dic
-rw-r--r--. 1 root root 419 6月  29 19:40 IKAnalyzer.cfg.xml
-rw-r--r--. 1 root root  34 6月  29 19:40 mydict.dic

第三步:
配置一個fieldType,指定使用IKAnalyzer(在schema.xml中編輯即可)
修改schema.xml,添加內(nèi)容如下:

<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

第四步:
配置業(yè)務域,需要用到中文檢索的那么type要指定上面自定義的fieldType
添加如下:

<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_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 是根據(jù)業(yè)務需要添加的,全文搜索時,需要用到商品標題,賣點,分類等等相關(guān)檢索信息,所以這里才配置了這么幾個表示商品標題,賣點,分類等的copyField 。

啟動tomcat查看:

image

查看商品標題分析值

image

接下來的就是后臺界面進行增刪改,查(重點)的操作,以及java代碼來實現(xiàn)增刪改,查(重點)了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 講過solr在windows和linux下的安裝了,但是并沒有安裝中文分析器,這里補充吧。安裝中文分詞器需要用到s...
    __豆約翰__閱讀 560評論 0 1
  • 上一篇的配置說明主要是說明solrconfig.xml配置中的查詢部分配置,在solr的功能中另外一個重要的功能是...
    堯字節(jié)閱讀 2,238評論 0 6
  • 在一件事上啥忙了兩天,好像耗盡了腦力,要休息一下,現(xiàn)在什么東西都看不進去,工作不在狀態(tài)。跑步弄了十天左右,...
    Chmod_777閱讀 349評論 0 0
  • 我有一個朋友,今年剛剛畢業(yè),然而事實上,去年她就可以申請實習而離開學校,但是她沒有,就像個不愿長大的孩子,在母體里...
    U塔閱讀 965評論 0 1

友情鏈接更多精彩內(nèi)容