Solr4:加入中文分词mmseg4j

前提是已经在Tomcat7下面配置好Solr4.0,详情参考:Solr4:Tomcat7下面配置Solr
1.下载mmseg4j
下载地址:http://code.google.com/p/mmseg4j/downloads/list,目前mmseg4j的mmseg4j-1.9.0-SNAPSHOT版本支持Solr4.0,但仍然需要做相关改动。
2.解压mmseg4j-1.9.0.v20120712-SNAPSHOT.zip
用到下面文件:
mmseg4j-all-1.9.0.v20120712-SNAPSHOT.jar 放到之前安装$CATALINA_HOME/webapps/solr/WEB-INF/lib/目录下
data 目录,建议拷贝下面内容到$SOLR_HOME\collection1\conf\mm4jdic
3.设置mmseg4j中文分词
修改$SOLR_HOME/collection1/conf/schema.xml,在<types></types>中增加如下内容: 

<fieldType name="textComplex" class="solr.TextField" >
   <analyzer>
     <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="C:/solr/collection1/conf/mm4jdic"/>
   </analyzer>
  </fieldType>
  <fieldType name="textMaxWord" class="solr.TextField" >
   <analyzer>
     <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="C:/solr/collection1/conf/mm4jdic"/>
   </analyzer>
  </fieldType>
  <fieldType name="textSimple" class="solr.TextField" >
   <analyzer>
     <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="C:/solr/collection1/conf/mm4jdic"/>
   </analyzer>
  </fieldType>  

修改$SOLR_HOME/collection1/conf/schema.xml文件,在<fields></fields>中修改如下内容:

  <field name="content" type="textSimple" indexed="false" stored="true" multiValued="true"/>
  <field name="text" type="textSimple" indexed="true" stored="false" multiValued="true"/>
   ......
  <copyField source="content" dest="text"/>

也可以自己增加删除相关字段。
4.修改mmseg4j的小BUG
以下内容参考:http://wxf4150.blog.163.com/blog/static/111380836201292911234802/
修改mmseg4j源代码中的MMSeg.java,增加属性readerStatus并增加相关Setters与Getters

 private int readerStatus;
 
 public int getReaderStatus() {
  return readerStatus;
 }

 public void setReaderStatus(int readerStatus) {
  this.readerStatus = readerStatus;
 }

修改mmseg4j源代码中的MMSegTokenizer.java,

 public void reset(Reader input) throws IOException {
  //super.reset(input);
  super.setReader(input);
  mmSeg.reset(input);
 }
 
 ......
 @Override
 public boolean incrementToken() throws IOException {
  clearAttributes();
  
  if(mmSeg.getReaderStatus() == 1) {
   mmSeg.reset(this.input);
   mmSeg.setReaderStatus(0);
  }
  
  Word word = mmSeg.next();
  if(word != null) {
   //lucene 3.0
   //termAtt.setTermBuffer(word.getSen(), word.getWordOffset(), word.getLength());
   //lucene 3.1
   termAtt.copyBuffer(word.getSen(), word.getWordOffset(), word.getLength());
   offsetAtt.setOffset(word.getStartOffset(), word.getEndOffset());
   typeAtt.setType(word.getType());
   return true;
  } else {
   end();
   
   mmSeg.setReaderStatus(1);
   
   return false;
  }
 }

上述两个文件编译后,加入到mmseg4j-all-1.9.0.v20120712-SNAPSHOT.jar包中。  
5.启动Tomcat,重建索引,查询即可。

详情参考之前的文章。

原文地址:https://www.cnblogs.com/nayitian/p/2866737.html