问题:
使用中科院的中文分词对“中华人民共和国” 进行索引,它被分词为"中华", "人民", "共和国",用“人民共和国”进行搜索,可以搜到,而搜索"中华共和国"却搜索不到,用“中华 AND 共和国”却可以搜出来,为什么?
回答:
我下载了http://ictclas.org/Download.html中科院的词做了简单的分析,如果索引的时候“中华人民共和国”被分成了“中华”“人民”“共和国”,而搜索的时候,搜“中华共和国”,则被分为了“中华 共和国”,然而构建Query Parser构建Query Object的时候,却将它构建成了PhraseQuery—— contents:"中华 共和国" ,而非BooleanQuery——contents:中华 contents:共和国,根据PhraseQuery的解释,它有一个参数slop来表示两个词之间的距离,默认为0,也即只有在文档不但包含“中华”而且包含“共和国”并且二者相邻的时候才能返回。这就是为什么“人民共和国”可以搜出来(它构建的是PhraseQuery,但是相邻),“中华 AND 共和国”能搜索出来(它构建的是BooleanQuery),而“中华共和国”搜不出来的原因(它构建的是PhraseQuery,但不相邻)。
尝试解析Query query = parser.parse("\"中华共和国\"~1")
或者用API设置Slop为1,就能搜索出结果了。
Query query = parser.parse("中华共和国"); PhraseQuery pquery = (PhraseQuery)query; pquery.setSlop(1); |
实例:
Analyzer ca = new ChineseAnalyzer();
QueryParser parser = new QueryParser(field, ca);
Query query1 = parser.parse("人民共和国");
System.out.println("Searching for: " + query1.toString(field));
查询对象为:
query1 PhraseQuery (id=39) boost 1.0 field "contents" maxPosition 1 positions ArrayList<E> (id=45) slop 0 terms ArrayList<E> (id=49) elementData Object[4] (id=74) [0] Term (id=76) field "contents" text "人民" [1] Term (id=77) field "contents" text "共和国"
|
相当于查询语句:
Query query2 = parser.parse("中华 AND 共和国");
System.out.println("Searching for: " + query2.toString(field));
查询对象为:
query2 BooleanQuery (id=43) boost 1.0 clauses ArrayList<E> (id=56) elementData Object[10] (id=57) [0] BooleanClause (id=59) occur BooleanClause$Occur (id=62) name "MUST" query TermQuery (id=65) boost 1.0 term Term (id=70) field "contents" text "中华" [1] BooleanClause (id=61) occur BooleanClause$Occur (id=62) name "MUST" query TermQuery (id=64) boost 1.0 term Term (id=68) field "contents" text "共和国"
|
相当于查询语句:
Query query3 = parser.parse("\"中华共和国\"~1");
System.out.println("Searching for: " + query3.toString(field));
查询对象为:
query3 PhraseQuery (id=54) boost 1.0 field "contents" maxPosition 1 positions ArrayList<E> (id=93) slop 1 terms ArrayList<E> (id=94) elementData Object[4] (id=96) [0] Term (id=97) field "contents" text "中华" [1] Term (id=98) field "contents" text "共和国"
|
相当于查询语句:
Searching for: "中华 共和国"~1 |
Query query4 = parser.parse("中华共和国");
PhraseQuery pquery = (PhraseQuery)query4;
pquery.setSlop(1);
System.out.println("Searching for: " + query4.toString(field));
查询对象为:
query4 PhraseQuery (id=55) boost 1.0 field "contents" maxPosition 1 positions ArrayList<E> (id=102) slop 1 terms ArrayList<E> (id=103) elementData Object[4] (id=105) [0] Term (id=107) field "contents" text "中华" [1] Term (id=108) field "contents" text "共和国"
|
相当于查询语句:
Searching for: "中华 共和国"~1 |
分享到:
相关推荐
Lucene_3.0_原理与代码分析
lucene3.6 搜索例子
lucene 高级搜索项目 附件搜索 附件内容搜索 全文搜索
Lucene实现全文搜索,支持英文、模糊和智能查询
lucene 近实时搜索 很清楚的解释了关于lucene近实时搜索的代码。很值得学习
主要是关于lucene站内搜索的技术代码,可以使用;按照需要进行代码修改。
赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...
基于Lucene的搜索策略研究
lucene 搜索中文文档,写的比较详细
全文检索介绍 索引 分词 Lucene介绍 Lucene应用详解 索引器 检索器 条件查询 实用工具及高亮器 Lucene综合应用——仿搜索引擎
Lucene.Heritrix:开发自己的搜索引擎(第2版).邱哲.扫描版 Lucene.Heritrix:开发自己的搜索引擎(第2版).邱哲.扫描版
本文介绍Lucene.Net 是什么?Lucene.Net 能作什么?以及怎么做的问题?最后给出 Lucene.Net 实现全文搜索的一个示例
c#下实现Lucene时间区间查询匹配。主要还是对Lucene查循对像Query的实现
这个程序是用java做的桌面搜索,利用lucene API技术完成建立索引跟搜索功能,其它大部分功能是仿照火速搜索软件的,请大家多多指教,因为没时间完善,所以还存在很多缺点,特别是界面运行就比较慢~
07.Lucene搜索实战1 共4页 08.Lucene搜索实战2 共5页 09.Lucene搜索深入实战1 共5页 10.Lucene搜索深入实战2 共11页 11.Lucene搜索深入实战进阶1 共4页 12.Lucene搜索深入实战进阶2 共9页 13.Lucene搜索深入实战进阶...
官网的lucene全文检索引擎工具包,下载后直接解压缩即可使用
07.Lucene搜索实战1 共4页 08.Lucene搜索实战2 共5页 09.Lucene搜索深入实战1 共5页 10.Lucene搜索深入实战2 共11页 11.Lucene搜索深入实战进阶1 共4页 12.Lucene搜索深入实战进阶2 共9页 13.Lucene搜索深入实战进阶...
利用lucene进行搜索,IndexSearcher是整个Lucene搜索查询相关信息的驱动引擎,在使IndexSearcher之前,需要构建IndexSearcher对象,Lucene提供了两种构建IndexSearcher对象的方式: 1、基于Directory对象构建; 2...
用lucene对数据库建立索引及搜索.doc
C#调用Lucene方法-实现快速搜索