关于solr不能搜索中文

来源:互联网 发布:linux怎么防御勒索 编辑:程序博客网 时间:2024/05/21 07:05

关于solr不能搜索中文

1、需要配置中文分词器,我在此使用的IKAnalyzer分词器,需要jar :IKAnalyzer3.2.8.jar

schema.xml

<fieldType name="text" class="solr.TextField"><analyzer type="index"><tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="false" /><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /><filter class="solr.LowerCaseFilterFactory" /></analyzer><analyzer type="query"><tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="true" /><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /><filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true" /><filter class="solr.LowerCaseFilterFactory" /></analyzer></fieldType>  

分词配好之后,我们可以再solr的管理页面的[ANALYSIS] 的页面来进行分词测试:


这样你输入中华的时候,就可以匹配到 “中华人民共和国”;上图你可以清楚的看到分词器如何将中华人民共和国分割的

通常我们为了搜索的方便,都用keyword来搜索:

 <fields>  <field name="id" type="sint" indexed="true" stored="true" required="true" />  <field name="userName" type="string" indexed="true" stored="true"/>  <field name="gender" type="string" indexed="true" stored="true"/>  <field name="address" type="string" indexed="true" stored="true" />  <field name="createTime" type="date" indexed="true" stored="true" default="NOW"/> <!-- 关键字--><field name="keyword" type="text" indexed="true" stored="true" multiValued="true" />  </fields> 

  <copyField source="gender" dest="keyword"/>    <copyField source="address" dest="keyword"/>    <copyField source="userName" dest="keyword"/>

这样你搜索keyword=xxx 的时候,那么就会去gender,address,userName里面去找符合的结果。


2、如果分词器也配置好了,还是搜索不出来中文(我就出现这样的问题),那可能就是solr的字符集和tomcat的字符集不统一

我输入男子的时候,我发现在tomcat里面出现一条请求


乱码,应该就是字符集的问题,solr的默认字符集为UTF-8;所以我将tomcat的字符集设置成UTF-8

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8443" />
重启tomcat

ok,解决问题