启动solr,运行techproducts示例,使用smartcn中文分词包,加载mmseg4j中文分词包(solr7.3.1)
1. 启动错误
在下载解压完solr后,遇到的第一个问题是启动不了solr,执行以下指令后
bin/solr start -e techproducts
出现如下错误:

经过查找资料,solr默认不允许root用户进行操作,而通过
ls -l
我们可以发现目录的拥有者是root

在非root账户下,在solr的上一级目录,使用如下指令修改solr目录的拥有者
sudo chown -R 用户:用户组 solr-7.3.1/
此时在启动solr,就会发现一切OK
2. 启动techproducts样例
输入以下指令启动样例
bin/solr start -e techproducts
启动成功

在本地电脑浏览器中输入以下地址访问Solr Admin UI(将localhost换成云服务器的公网IP,有风险,只要他人知道公网IP,即可操作你的solr)
http://localhost:8983/solr

基础查询

分面查询
界面上找不到的参数在下面的方框中输入



网页查询页面(包括分面查询)

3. Solr配置中文分词包smartcn
- 确定smartcn所在位置
/usr/src/solr-7.3.1/contrib/analysis-extras/lucene-libs
- 在/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/conf下的solrconfig.xml中添加如下语句,加载smartcn包
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lucene-libs/"
regex="lucene-analyzers-smartcn-\d.*\.jar" />
- 在/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/conf下的managed-schema中,新建一个字段类型text_chinese,用以处理中文分词。
<fieldType name="text_chinese" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
</fieldType>
中文分词效果

中英文混合分词效果

新词处理

从新词处理可以看出smartcn有很大的缺陷,对于新的中文词,只能将其按字进行分词。
4. Solr配置中文分词包mmseg4j和词典文件
- 将mmseg4j-core-1.10.0.jar与mmseg4j-solr-2.4.0.jar两个jar包复制如下文件夹(若无lib,则新建lib目录)
/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/lib -
从mmseg4j-core-1.10.0.jar的data文件夹中提取出的3个词典文件放入/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/dic,如下图:
image
- 修改managed-schema文件,添加3个新的中文分词字段类型和指定词典路径:
<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex"
dicPath="/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/dic"/>
</analyzer>
</fieldtype>
<fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word"
dicPath="/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/dic"/>
</analyzer>
</fieldtype>
<fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple"
dicPath="/usr/src/solr-7.3.1/example/techproducts/solr/techproducts/dic" />
</analyzer>
</fieldtype>
- 重启solr服务器
- 进入solr Amdin界面,使用textSimple模式
image
- 如上所示,小目标、老司机、洪荒之力、吃瓜群众、蓝瘦香菇等词被拆分,在word.dic文件末尾,根据我们目标的分词效果,添加自定义词汇,添加完之后,再次重启solr服务器
image
最终的分词效果如图:
image
网友评论