Solr集成IKAnalyzer中文分词器

前言

官网:
https://code.google.com/archi…

IK Analyzer 2012 FF版本 (即For 4.0),在API和功能上保持不变,只是让其支持了Lucene4.0和Solr4.0,让这部分的用户能用起来。

如果你还是Lucene3.2-3.6的用户,那么你只需要下载IK Analyzer 2012 U6版本。因为FF版本的API与3.x是不兼容的。

【IK Analyzer 安装包包含】:

  1. 《IKAnalyzer 中文分词器 V2012 使用手册》(即本文档)
  2. IKAnalyzer2012.jar(主 jar 包)
  3. IKAnalyzer.cfg.xml(分词器扩展配置文件)
  4. stopword.dic(停止词典)
  5. LICENSE.TXT ; NOTICE.TXT (apache 版权申明)

它的安装部署十分简单 , 将 IKAnalyzer2012.jar 部署于项目的lib目录中;IKAnalyzer.cfg.xml 与 stopword.dic 文件放置在 class 根目录(对于 web 项目,通常是WEB-INF/classes 目录,同 hibernate、log4j 等配置文件相同)下即可。

1. 下载安装

最新版本:IK Analyzer 2012

# 一定要下载FF版本,因为使用的是solr4.0以上版本
$ wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/ik-analyzer/IK%20Analyzer%202012FF_hf1.zip

#解压到IK2012目录中,并且不覆盖相同文件
$ unzip -n IKAnalyzer2012_u6.zip -d IK2012

#拷贝jar包到tomcat下solr的工程目录中
$ cp IK2012/IKAnalyzer2012FF_u1.jar /opt/tomcat-8.5.31/webapps/solr/WEB-INF/lib/

#创建classes文件夹
$ mkdir /opt/tomcat-8.5.31/webapps/solr/WEB-INF/classes

# 拷贝IKAnalyzer.cfg.xml和stopword.dic到classes文件夹下
$ cp IKAnalyzer.cfg.xml /opt/tomcat-8.5.31/webapps/solr/WEB-INF/classes/
$ cp stopword.dic /opt/tomcat-8.5.31/webapps/solr/WEB-INF/classes/

2. 修改配置

修改solr core中schema文件,默认位置:

$ vim /opt/solr-4.10.3/example/solr/collection1/conf/schema.xml 

添加如下配置:

<fieldType name="text_ik" class="solr.TextField">  
   <!-- 索引时候的分词器 -->
   <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
   <!-- 查询时候的分词器 -->
   <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
</fieldType>

同时,把需要分词的字段,设置为text_ik

<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
<field name="category" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="content" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
<field name="price" type="double" indexed="true" stored="true" required="true" multiValued="false" />
<field name="color" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="orderBy" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="updatetime" type="date" indexed="true" stored="true" required="true" multiValued="false" />

3. 重启服务

注意:如果之前已经创建了索引,需要将之前的索引删掉,重新创建分词后的索引。

$ /opt/tomcat-8.5.31/bin/shutdown.sh
$ /opt/tomcat-8.5.31/bin/startup.sh

4. 配置扩展词典

1.默认是用的IKAnalyzer分词器内置的词典进行分词的。我们也可以自己配置IKAnalyzer分词器的扩展词典

# 修改IKAnalyzer.cfg.xml文件
$ vim /opt/tomcat-8.5.31/webapps/solr/WEB-INF/classes/IKAnalyzer.cfg.xml

配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
  <comment>IK Analyzer 扩展配置</comment>
  <!--用户可以在这里配置自己的扩展字典,表示使用哪些词来做索引 -->
  <entry key="ext_dict">ext.dic;</entry> 
  <!--用户可以在这里配置自己的扩展停止词字典,表示不用哪些词做索引-->
  <entry key="ext_stopwords">stopword.dic;</entry> 
</properties>

2.在classes下创建 ext.dic 来配置字典(每一行表示一个整体索引)

$ vim /opt/tomcat-8.5.31/webapps/solr/WEB-INF/classes/ext.dic 

配置如下:

别看我乱我就是索引
哈哈哈
我是第三行

stopword.dic 和 ext.dic 的编码方式为UTF-8 无BOM的编码方式。

3.重启tomcat后测试

《Solr集成IKAnalyzer中文分词器》

    原文作者:Developer
    原文地址: https://segmentfault.com/a/1190000014780445
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞