solr中文分词mmseg4j详细配置



1、下载mmseg4j-1.9.1并解压mmseg4j-1.9.1.zip,把dist下面的所有jar文件拷贝到你应用服务器下的 solr /WEB-INF/lib中。

有3个jar文件:mmseg4j-analysis-1.9.1.jar, mmseg4j-core-1.9.1.jar,mmseg4j-solr-1.9.1.jar。

顺便提下,如果是在mmseg4j-1.9.0前, 则需要copy data目录到solr_home/solr中(与core平级),并改名为dic。进入到你想使用mmseg4j分词器的core中(此处以solr自带的collection1为例),用编辑器打开collection1/conf/schema.xml配置文件,添加如下代码:

1<!-- mmseg4j分词器 -->
2 <fieldType name="text_mmseg4j" class="solr.TextField" >
3 <analyzer type="index">
4 <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="../dic" /><!--此处为分词器词典所处位置-->
5 </analyzer>
6 <analyzer type="query">
7 <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="../dic" /><!--此处为分词器词典所处位置-->
8 </analyzer>
9 </fieldType>

2、 在mmseg4j-1.9.0后,如本例的mmseg4j-1.9.1中 ,就 可以不用 dicPath 参数,可以使用 mmseg4j-core-1.9.0.jar 里的 words.dic ,在Schema.xml中加入如下配置

1<!-- mmseg4j-->
2  <fieldType name="text_mmseg4j_complex" class="solr.TextField" positionIncrementGap="100"
3    <analyzer
4      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="dic"/> 
5    </analyzer
6  </fieldType
7  <fieldType name="text_mmseg4j_maxword" class="solr.TextField" positionIncrementGap="100"
8    <analyzer
9      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="dic"/> 
10    </analyzer
11  </fieldType
12  <fieldType name="text_mmseg4j_simple" class="solr.TextField" positionIncrementGap="100"
13    <analyzer
14      <!--
15      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="n:/OpenSource/apache-solr-1.3.0/example/solr/my_dic"/>
16      -->
17      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="dic"/>  
18    </analyzer
19  </fieldType>
20  <!-- mmseg4j-->

3、引用mmseg4j分词器

只需要在该schema.xml中加入如下配置便可引用对应的mmseg4j分词器

1<field name="mmseg4j_complex_name" type="text_mmseg4j_complex" indexed="true" stored="true"/>
2<field name="mmseg4j_maxword_name" type="text_mmseg4j_maxword" indexed="true" stored="true"/>
3<field name="mmseg4j_simple_name" type="text_mmseg4j_simple" indexed="true" stored="true"/>

通过以上步骤就可以成功配置mmseg4j分词器到solr中了。

注:以下异常在http://pan.baidu.com/s/1pJCyAd9下载的文件已处理。

然后就可以打开Solr Admin的Page进行分词分析了。但当输入中文(华南理工大学)并点击“Analyse Values”进行分析时,会发现如下的错误:  TokenStream contract violation: reset()/close() call missing, reset() called multiple times, or subclass does not call super.reset(). Please see Javadocs of TokenStream class for more information about the correct consuming workflow.

该原因是源码的一个bug引起的,需要修改上面下载的mmseg4j-analysis-1.9.1.zip解压后的mmseg4j-analysis目录下的类:MMSegTokenizer.java,修改reset()方法并加上下面注释中的这一句

1public void reset() throws IOException {
2//lucene 4.0
3//org.apache.lucene.analysis.Tokenizer.setReader(Reader)
4//setReader 自动被调用, input 自动被设置。
5super.reset(); //加这一句
6mmSeg.reset(input);
7}

修改后运行mvn clean package -DskipTests进行打包得到最新的mmseg4j-analysis-1.9.1.jar 并替换Tomcat下的solr下的WEB-INF/lib下的mmseg4j-analysis-1.9.1.jar。

重新启动Tomcat并访问Solr Admin Page,并在“Analysis”中输入中文进行分析,可以看到已经成功的进行分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值