原文出处:http://blog.chenlb.com/2010/08/get-solr-analysis-word.html
Solr 1.3 只有 AnalysisRequestHandler 处理器,只能提交文档来观察文档的分词结果。 Solr 1.4 有了对字段的分词。FieldAnalysisRequestHandler 可以对某个字段或字段类型的分词器对查询串取到分词数据。
用 solr 的默认配置,如 solr 1.4.1。
我用 mmseg4j 为例。在 solr.root/example/solr/conf/schema.xml 的 types 元素内加:
- <fieldType name="text_cn" class="solr.TextField" positionIncrementGap="100">
- <analyzer>
- <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory"/>
- </analyzer>
- </fieldType>
<fieldType name="text_cn" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory"/> </analyzer> </fieldType>
把 mmseg4j-all-1.8.2-with-dic.jar 放到 solr.home/example/solr/lib,没有 lib 目录,创建一个。更多关于 solr 中使用 mmseg4j 的内容请看:solr 中文分词 mmseg4j 使用例子。
- {
- "responseHeader":{
- "status":0,
- "QTime":0},
- "analysis":{
- "field_types":{
- "text_cn":{
- "query":[
- "com.chenlb.mmseg4j.analysis.MMSegTokenizer",[{
- "text":"中国",
- "type":"word",
- "start":0,
- "end":2,
- "position":1},
- {
- "text":"工商",
- "type":"word",
- "start":2,
- "end":4,
- "position":2},
- {
- "text":"银行",
- "type":"word",
- "start":4,
- "end":6,
- "position":3}]]}},
- "field_names":{}}}
{ "responseHeader":{ "status":0, "QTime":0}, "analysis":{ "field_types":{ "text_cn":{ "query":[ "com.chenlb.mmseg4j.analysis.MMSegTokenizer",[{ "text":"中国", "type":"word", "start":0, "end":2, "position":1}, { "text":"工商", "type":"word", "start":2, "end":4, "position":2}, { "text":"银行", "type":"word", "start":4, "end":6, "position":3}]]}}, "field_names":{}}}
就可以取得查询串的分词结果。
这个功能可以解决,如搜索“清华大学” 找不到 “清华 大学” 文档的问题。这个问题的根本原因是 lucene / solr 使用的查询解析器生成的 Query 是短语查询。短语查询默认又是连续的词中没有其它字符。所以会找不到。
目前我知的方式大概有二种:
1、查询前分词一遍,把分出的词用空格分开,再去搜索(叫它为查询预处理)。查询前分词可以上面的接口。
2、扩展 solr query parser,返回 boolean query。
当然还有同学回复说指定短语的距离,如 "清华大学~100",这个可以投机使用,不够可靠。
本文详细介绍了如何在Solr 1.4中使用mmseg4j进行中文分词,通过配置schema.xml文件并添加mmseg4j-all-1.8.2-with-dic.jar至lib目录,实现对特定字段类型的分词处理,解决了搜索短语时的匹配问题。
995

被折叠的 条评论
为什么被折叠?



