前言
官網:
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 安裝包包含】:
- 《IKAnalyzer 中文分詞器 V2012 使用手冊》(即本文檔)
- IKAnalyzer2012.jar(主 jar 包)
- IKAnalyzer.cfg.xml(分詞器擴展配置文件)
- stopword.dic(停止詞典)
- 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後測試