1. 程式人生 > >Solr6配置中文分詞庫mmseg4j

Solr6配置中文分詞庫mmseg4j

摘要: Solr有諸多分詞器,本文介紹Solr6與中文分詞庫mmseg4j的整合,在此之前,你需要有一個可以執行Solr的環境,參見Solr6.0與Jetty、Tomcat在Win環境下搭建/部署

準備環境

mmseg4j需要mmseg4j-core-1.10.0.jar和mmseg4j-solr-2.3.0.jar,之前的mmseg4j-analysis已經整合進了mmseg4j-solr-2.3.0.jar,不需要再匯入
<dependency>
    <groupId>com.chenlb.mmseg4j</groupId>
    <artifactId
>
mmseg4j-core</artifactId> <version>1.10.0</version> </dependency> <dependency> <groupId>com.chenlb.mmseg4j</groupId> <artifactId>mmseg4j-solr</artifactId> <version>2.3.0</version> </dependency>

科普mmseg4j

  1. mmseg4j 用 Chih-Hao Tsai 的 MMSeg 演算法(
    http://technology.chtsai.org/mmseg/
    )實現的中文分詞器,並實現 lucene 的 analyzer 和 solr 的TokenizerFactory 以方便在Lucene和Solr中使用。
  2. MMSeg 演算法有兩種分詞方法:Simple和Complex,都是基於正向最大匹配。
  3. mmseg4j有三種分詞模式simple|complex|max-word,預設是max-word。
  4. mmseg4j的詞庫強制使用 UTF-8。
  5. mmseg4j 1.8.3 只支援 lucene 2.9/3.0 介面 和 solr 1.4。
    mmseg4j 1.8.5 支援 lucene 3.1, solr 3.1。
    mmseg4j 1.9.0 支援 lucene 4.0, solr 4.0。
    mmseg4j 1.9.1 支援 solr/lucene 4.3.1。
    mmseg4j-solr-2.0.0.jar 要求 lucene/solr >= 4.3.0。
    mmseg4j-solr-2.1.0.jar 要求 lucene/solr 4.8.x。
    mmseg4j-solr-2.2.0.jar 要求 lucene/solr [4.9, 4.10.x]。
    mmseg4j-solr-2.3.0.jar 要求 lucene/solr [5.0, ]
  6. 作者chenlb,chenlb是中文名拼音Chen Lin Bin簡寫,公開資料顯示其來自浙江杭州。許多關於mmseg4j的說明可以在他的部落格上找到。很可惜的是,現在mmseg4j已經沒有更新了,並且mmseg4j已經不能完整支援Solr6及以上版本。

Solr6整合中文分詞mmseg4j

  1. 確保已經裝好了Solr6,如果版本高於6,可能會不支援,需要改mmseg4j包
  2. 解壓下載的壓縮包mmseg4j-solr-2.3.0-with-mmseg4j-core.zip,得到mmseg4j-core-1.10.0.jarmmseg4j-solr-2.3.0.jar
  3. 在solr_home下新建資料夾lib,將兩個jar檔案複製進去。
    這裡寫圖片描述
  4. 配置schema.xml:在solr_home/core0/conf下找到schema.xml.bak檔案,將其重新命名為schema.xml刪除managed-schema。
    這裡寫圖片描述
  5. 編輯schema.xml:新增fieldType 和 field到schema.xml檔案。
<!-- mmseg4j fieldType-->
    <fieldType name="text_mmseg4j_complex" class="solr.TextField" positionIncrementGap="100" >  
        <analyzer>  
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" />  
        </analyzer>  
    </fieldType>  
    <fieldType name="text_mmseg4j_maxword" class="solr.TextField" positionIncrementGap="100" >  
        <analyzer>  
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />  
        </analyzer>  
    </fieldType>  
    <fieldType name="text_mmseg4j_simple" class="solr.TextField" positionIncrementGap="100" >  
        <analyzer>  
          <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" />     
        </analyzer>  
    </fieldType>

注意:請不要給tokenizer加”dicPath”屬性,因為當前版本2.3.0的mmseg4j已經不能很好支援Solr6,新版本的Solr中有API的改動

<!--mmseg4j field-->
   <field name="mmseg4j_complex" type="text_mmseg4j_complex" indexed="true" stored="true"/>
   <field name="mmseg4j_maxword" type="text_mmseg4j_maxword" indexed="true" stored="true"/>
   <field name="mmseg4j_simple" type="text_mmseg4j_simple" indexed="true" stored="true"/>

6.啟動Solr,如果控制檯沒有報錯,那就大功告成了。點選Analysis,測試幾個資料看看。下面這個沒有分詞成南京-市長-江大橋
這裡寫圖片描述
7.如果你沒有執行起來,可能遇到了不相容的問題,在下一篇解答。