solr中文分词(mmseg4j) 编辑
1、从http://code.google.com/p/mmseg4j/ 下载mmseg4j2、在$SOLR_HOME下建立lib和dic两个目录,讲mmseg4j-all-1.8.4.jar拷贝到lib目录,将data里的.dic文件拷贝到dic目录
3、修改Schema.xml
添加fieldType
Xml代码
[*]<types>
[*]<fieldTypename="textComplex"class="solr.TextField"positionIncrementGap="100">
[*]<analyzer>
[*]<tokenizerclass="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory"mode="complex"dicPath="/opt/solr/example/solr/dic"/>
[*]<filterclass="solr.LowerCaseFilterFactory"/>
[*]</analyzer>
[*]</fieldType>
[*]
[*]<fieldTypename="textMaxWord"class="solr.TextField"positionIncrementGap="100">
[*]<analyzer>
[*]<tokenizerclass="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory"mode="max-word"dicPath="/opt/solr/example/solr/dic"/>
[*]<filterclass="solr.LowerCaseFilterFactory"/>
[*]</analyzer>
[*]</fieldType>
[*]
[*]<fieldTypename="textSimple"class="solr.TextField"positionIncrementGap="100">
[*]<analyzer>
[*]<tokenizerclass="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory"mode="simple"dicPath="/opt/solr/example/solr/dic"/>
[*]<filterclass="solr.LowerCaseFilterFactory"/>
[*]</analyzer>
[*]</fieldType>
[*]
[*]..
[*]</types>
Xml代码
[*]<fieldname="simple"type="textSimple"indexed="true"stored="true"multiValued="true"/>
[*]<fieldname="complex"type="textComplex"indexed="true"stored="true"multiValued="true"/>
[*]<fieldname="maxword"type="textMaxWord"indexed="true"stored="true"multiValued="true"/>
Xml代码
[*]<copyFieldsource="simple"dest="text"/>
[*]<copyFieldsource="complex"dest="text"/>
重启tomcat
进入 http://yourhost:8080/solr-example/admin/analysis.jsp
哦也,我们的中文分词大功告成了
我们试着提交些中文到solr里,然后进行查询
Xml代码
[*]chinese.xml
[*]<add>
[*]<doc>
[*]<fieldname="id">1</field>
[*]<fieldname="title">夜晚和白天不同,如果相机设置不准确的话,照片拍出来就会发糊。那么本期佳能单反课堂就带您详细了解夜景拍摄的参数设置,同时为您讲解什么叫做“安全快门”。除此之外,还有更多新奇有趣的特殊拍摄手法,还等什么?马上进入本期的节目吧!</field>
[*]</doc>
[*]<doc>
[*]<fieldname="id">2</field>
[*]<fieldname="title">冰动娱乐自主研发的虚幻3即时回合制网络游戏!UnrealEngine3倾力打造、最终幻想式的创新玩法以及天马行空般的幻想三国题材将带给你耳目一新的全新感受。</field>
[*]</doc>
[*]<doc>
[*]<fieldname="id">3</field>
[*]<fieldname="title">solr是基于LuceneJava搜索库的企业级全文搜索引擎,目前是apache的一个项目。</field>
[*]</doc>
[*]<doc>
[*]<fieldname="id">4</field>
[*]<fieldname="title">中国人民银行是中华人民共和国的中央银行。</field>
[*]</doc>
[*]</add>
我们用curl进行提交
命令行代码
[*]curl'http://localhost:8080/solr-example/update/?commit=true'-H"Content-Type:text/xml"--data-binary@chinese.xml
接下来我们试着查询一下:
查询结果
Xml代码
[*]<response>
[*]<lstname="responseHeader">
[*]<intname="status">0</int>
[*]<intname="QTime">2</int>
[*]<lstname="params">
[*]<strname="indent">on</str>
[*]<strname="start">0</str>
[*]<strname="q">title:单反</str>
[*]<strname="rows">10</str>
[*]<strname="version">2.2</str>
[*]</lst>
[*]</lst>
[*]<resultname="response"numFound="1"start="0">
[*]<doc>
[*]<strname="id">1</str>
[*]<arrname="title"><str>夜晚和白天不同,如果相机设置不准确的话,照片拍出来就会发糊。那么本期佳能单反课堂就带您详细了解夜景拍摄的参数设置,同时为您讲解什么叫做“安全快门”。除此之外,还有更多新奇有趣的特殊拍摄手法,还等什么?马上进入本期的节目吧!</str></arr>
[*]</doc>
[*]</result>
[*]</response>
可能会遇到的问题:
1、在Query String:输入中文时候会乱码导致查询不到结果
解决办法:修改tomcat的server.xml
Xml代码
[*]<Server...>
[*]<Service...>
[*]<Connector...URIEncoding="UTF-8"/>
[*]...
[*]</Connector>
[*]</Service>
[*]</Server>
页:
[1]