全文检索Solr集成HanLP中文分词【转】

以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在Github上,欢迎改进。

HanLP中文分词solr插件支持Solr5.x,兼容Lucene5.x。

快速上手

  1. hanlp-portable.jarhanlp-solr-plugin.jar共两个jar放入${webapp}/WEB-INF/lib

  2. 修改solr core的配置文件${core}/conf/schema.xml

<fieldType name="text_cn" class="solr.TextField">    <analyzer type="index" enableIndexMode="true" class="com.hankcs.lucene.HanLPAnalyzer"/>    <analyzer type="query" enableIndexMode="true" class="com.hankcs.lucene.HanLPAnalyzer"/></fieldType>

效果一览

对于新手来说,上面的两步可能太简略了,不如看看下面的step by step

启动solr

首先在solr-5.2.1in目录下启动solr:

solr start -f

用浏览器打开http://localhost:8983/solr/#/,看到如下页面说明一切正常:

创建core

在solr-5.2.1serversolr下新建一个目录,取个名字比如叫one,将 示例配置文件solr-5.2.1serversolrconfigsetssample_techproducts_configsconf 拷贝过来,对conf目录下的schema.xml做上述一步改动,意思是使用HanLP分词器来对text_cn域进行分词。接着修改 schema.xml中的默认域type,搜索

<field name="text" type="text" indexed="true" stored="false" multiValued="true"/>

修改为

<field name="text" type="text_cn" indexed="true" stored="false" multiValued="true"/>

意思是默认文本为text_cn类型。

完成了之后在solr的管理界面导入这个core one:

接着就能在下拉列表中看到这个core了:

上传测试文档

修改好了,就可以拿一些测试文档来试试效果了。hanlp-solr-plugin代码库中的src/test/resources下有个测试文档集合documents.csv,其内容如下:

id,title1,你好世界2,商品和服务3,和服的价格是每镑15便士4,服务大众5,hanlp工作正常

代表着id从1到5共五个文档,接下来复制solr-5.2.1exampleexampledocs下的上传工具post.jar到resources目录,利用如下命令行将数据导入:

java -Dc=one -Dtype=application/csv -jar post.jar *.csv

Windows用户的话直接双击该目录下的upload.cmd即可。

正常情况下输出如下结果:

SimplePostTool version 5.0.0Posting files to [base] url http://localhost:8983/solr/one/update using content-type application/csv...POSTing file documents.csv to [base]1 files indexed.COMMITting Solr index changes to http://localhost:8983/solr/one/update...Time spent: 0:00:00.059请按任意键继续. . .

同时刷新一下core one的Overview,的确看到了5篇文档:

搜索文档

是时候看看HanLP分词的效果了,点击左侧面板的Query,输入“和服”试试:

发现精确地查到了“和服的价格是每镑15便士”,而不是“商品和服务”这种错误文档:

这说明HanLP工作良好。

要知道,不少中文分词器眉毛胡子一把抓地命中“商品和服务”这种错误文档,降低了查准率,拉低了用户体验,跟原始的MySQL LIKE有何区别?

代码调用

在Query改写的时候,可以利用HanLPAnalyzer分词结果中的词性等属性,如

String text = "中华人民共和国很辽阔";for (int i = 0; i < text.length(); ++i){    System.out.print(text.charAt(i) + "" + i + " ");}System.out.println();Analyzer analyzer = new HanLPAnalyzer();TokenStream tokenStream = analyzer.tokenStream("field", text);tokenStream.reset();while (tokenStream.incrementToken()){    CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class);    // 偏移量    OffsetAttribute offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);    // 距离    PositionIncrementAttribute positionAttr = kenStream.getAttribute(PositionIncrementAttribute.class);    // 词性    TypeAttribute typeAttr = tokenStream.getAttribute(TypeAttribute.class);    System.out.printf("[%d:%d %d] %s/%s
", offsetAtt.startOffset(), offsetAtt.endOffset(), positionAttr.getPositionIncrement(), attribute, typeAttr.type());}

在另一些场景,支持以自定义的分词器(比如开启了命名实体识别的分词器、繁体中文分词器、CRF分词器等)构造HanLPTokenizer,比如:

tokenizer = new HanLPTokenizer(HanLP.newSegment()                    .enableJapaneseNameRecognize(true)                    .enableIndexMode(true), null, false);tokenizer.setReader(new StringReader("林志玲亮相网友:确定不是波多野结衣?"));...

高级配置

HanLP分词器主要通过class path下的hanlp.properties进行配置,请阅读HanLP自然语言处理包文档以了解更多相关配置,如:

  1. 停用词

  2. 用户词典

  3. 词性标注

  4. ……

原文地址:http://www.hankcs.com/nlp/segment/full-text-retrieval-solr-integrated-hanlp-chinese-word-segmentation.html

原文地址:https://www.cnblogs.com/chenghu/p/6202355.html