- 浏览: 284668 次
- 性别:
- 来自: 湖南岳阳
-
最新评论
-
ternus:
兄弟,我用boboBrowse 也遇到了排序的问题,上线了讨论 ...
lucene 分组 bobo-Browse 排序的问题 -
luli0822:
Awesome bookmarks of those guru ...
流行的jQuery信息提示插件(jQuery Tooltip Plugin) -
shenbai:
如果你要在前台运行,你应该run得是ElasticSearch ...
ElasticSearch 源码分析 环境入门 -
cl1154781231:
<s:peroperty value="#at ...
关于Struts2中标签的一些心得 -
RonQi:
转载的吗?http://blog.csdn.net/stray ...
利用bobo-browse 实现lucene的分组统计功能
关于MultiTermQuery查询。
这里研究继承自MultiTermQuery的WildcardQuery查询。
WildcardQuery查询,就是使用通配符进行查询,通配符可以使用“*”和“?”这两种:“*”可以代表0~N个字符串,“?”只能代表一个字符串,而且它们可以在一个词条Term的任何位置出现,从WildcardQuery的构造方法中可以看出:
public WildcardQuery(Term term) {
super(term);
this.termContainsWildcard = (term.text().indexOf('*') != -1) || (term.text().indexOf('?') != -1);
}
使用通配符,是在构造完词条以后进行通配,然后根据使用通配符构造的词条,再构造一个WildcardQuery实例,接着就可以用这个WildcardQuery实例进行检索了。
WildcardQuery的使用非常简单,测试也非常容易。
1、使用“*”通配符
package org.apache.lucene.shirdrn.main;
import java.io.IOException;
import java.util.Date;
import net.teamhot.lucene.ThesaurusAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.LockObtainFailedException;
public class WildcardQuerySearcher {
private String path = "E:\\Lucene\\index";
private WildcardQuery wildcardQuery;
public void createIndex(){
IndexWriter writer;
try {
writer = new IndexWriter(path,new ThesaurusAnalyzer(),true);
Field fieldA = new Field("contents","文人",Field.Store.YES,Field.Index.TOKENIZED);
Document docA = new Document();
docA.add(fieldA);
Field fieldB = new Field("contents","文修武偃",Field.Store.YES,Field.Index.TOKENIZED);
Document docB = new Document();
docB.add(fieldB);
Field fieldC = new Field("contents","文东武西",Field.Store.YES,Field.Index.TOKENIZED);
Document docC = new Document();
docC.add(fieldC);
Field fieldD = new Field("contents","不使用武力",Field.Store.YES,Field.Index.TOKENIZED);
Document docD = new Document();
docD.add(fieldD);
Field fieldE = new Field("contents","不文不武",Field.Store.YES,Field.Index.TOKENIZED);
Document docE = new Document();
docE.add(fieldE);
writer.addDocument(docA);
writer.addDocument(docB);
writer.addDocument(docC);
writer.addDocument(docD);
writer.addDocument(docE);
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void useStarMatchExample(){ // 使用“*”通配符
Term term = new Term("contents","文*");
wildcardQuery = new WildcardQuery(term);
}
public void useCompositeMatchExample(){
Term term = new Term("contents","?*武*"); // 使用“*”和“?”组合的通配符
wildcardQuery = new WildcardQuery(term);
}
public static void main(String[] args) {
WildcardQuerySearcher wqs = new WildcardQuerySearcher();
wqs.createIndex();
wqs.useStarMatchExample();
// 调用使用“*”通配符设置的方法
try {
Date startTime = new Date();
IndexSearcher searcher = new IndexSearcher(wqs.path);
Hits hits = searcher.search(wqs.wildcardQuery);
System.out.println("********************************************************************");
for(int i=0;i<hits.length();i++){
System.out.println("Document的内部编号为 : "+hits.id(i));
System.out.println("Document内容为 : "+hits.doc(i));
System.out.println("Document的得分为 : "+hits.score(i));
}
System.out.println("********************************************************************");
System.out.println("共检索出符合条件的Document "+hits.length()+" 个。");
Date finishTime = new Date();
long timeOfSearch = finishTime.getTime() - startTime.getTime();
System.out.println("本次搜索所用的时间为 "+timeOfSearch+" ms");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
构造 WildcardQuery是在useStarMatchExample()方法中:
public void useStarMatchExample(){ // 使用“*”通配符
Term term = new Term("contents","文*");
wildcardQuery = new WildcardQuery(term);
}
检索结果自己也能猜想到,如下所示:
********************************************************************
Document的内部编号为 : 0
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文
人>>
Document的得分为 : 1.0
Document的内部编号为 : 1
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文
修武偃>>
Document的得分为 : 1.0
Document的内部编号为 : 2
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文
东武西>>
Document的得分为 : 1.0
********************************************************************
共检索出符合条件的Document 3 个。
本次搜索所用的时间为 313 ms
2、使用“*”和“?”组合通配符
在方法useCompositeMatchExample()中进行构造:
public void useCompositeMatchExample(){
Term term = new Term("contents","?*武*"); // 使用“*”和“?”组合的通配符
wildcardQuery = new WildcardQuery(term);
}
使用“*”和“?”组合的通配符进行构造Term,只要将上面测试函数中的wqs.useStarMatchExample();替换成为:
wqs.useCompositeMatchExample();
执行检索,结果可想而知:
********************************************************************
Document的内部编号为 : 1
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文修武偃>>
Document的得分为 : 0.9581454
Document的内部编号为 : 2
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:文东武西>>
Document的得分为 : 0.9581454
Document的内部编号为 : 3
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:不使用武力>>
Document的得分为 : 0.9581454
Document的内部编号为 : 4
Document内容为 : Document<stored/uncompressed,indexed,tokenized<contents:不文不武>>
Document的得分为 : 0.9581454
********************************************************************
共检索出符合条件的Document 4 个。
本次搜索所用的时间为 281 ms
简单总结
在以下的7篇文章中:
Lucene-2.2.0 源代码阅读学习(30) 、Lucene-2.2.0 源代码阅读学习(31) 、Lucene-2.2.0 源代码阅读学习(32) 、Lucene-2.2.0 源代码阅读学习(33) 、Lucene-2.2.0 源代码阅读学习(34) 、Lucene-2.2.0 源代码阅读学习(35) 、Lucene-2.2.0 源代码阅读学习(36)
以及在本文,学习了Query的一些重要的基础的实现查询的工具类,在熟练运用的基础上,综合各种查询,一定能够构造出一种非常复杂的查询,来满足实际的需求。
单独的一种Query是不可能满足用户的要求的。
发表评论
-
全文检索的基本原理
2010-02-25 10:22 876一、总论 根据http://lucene.apache.or ... -
lucene 分组 bobo-Browse 排序的问题
2010-02-01 16:18 2224今天碰到了一个问题,用bobo分组后对价格升序 居然100 ... -
开源搜索引擎
2010-02-01 14:31 1695开放源代码搜索引擎为 ... -
lucene中的filter器群组及其缓存大盘点
2010-01-20 23:18 1202lucene中的filter其实并不起眼,大家对其对性能的影响 ... -
利用bobo-browse 实现lucene的分组统计功能
2010-01-18 17:50 2940bobo-browse 是一用java写的lucene扩展组件 ... -
lucene Field部分参数设置含义
2009-11-07 17:51 1249<script type="text/ja ... -
刚下载,开始学习lucene时看的文章
2009-09-04 18:43 1431Lucene 2.0.0下载安装及测试 【下载】 下载链接 ... -
Lucene-2.3.1 阅读学习(42)
2009-09-04 18:42 945关于Hits类。 这个Hits类 ... -
Lucene 2.3.1 阅读学习(41)
2009-09-04 18:42 1412当执行Hits htis = search(query);这一 ... -
Lucene-2.3.1 源代码阅读学习(40)
2009-09-04 18:41 989关于Lucene检索结果的排序问题。 已经知道,Lucene ... -
Lucene-2.3.1 源代码阅读学习(39)
2009-09-04 18:41 1155关于Lucene得分的计算。 在IndexSearcher类 ... -
Lucene-2.3.1 源代码阅读学习(39)
2009-09-04 18:38 565关于Lucene得分的计算。 在IndexSearcher类 ... -
Lucene-2.3.1 源代码阅读学习(38)
2009-09-04 18:38 922关于QueryParser。 QueryParser是用来解 ... -
Lucene-2.3.1 源代码阅读学习(36)
2009-09-04 18:37 808关于MultiTermQuery查询。 ... -
Lucene-2.3.1 源代码阅读学习(35)
2009-09-04 18:36 842关于MultiPhraseQuery(多短语查询)。 Mul ... -
Lucene-2.3.1 源代码阅读学习(34)
2009-09-04 18:36 640关于PhraseQuery。 PhraseQuery查询是将 ... -
Lucene-2.3.1 源代码阅读学习(33)
2009-09-04 18:35 870关于范围查询RangeQuery。 ... -
Lucene-2.3.1 源代码阅读学习(32)
2009-09-04 18:35 1156关于SpanQuery(跨度搜索),它是Query的子类,但是 ... -
Lucene-2.3.1 源代码阅读学习(31)
2009-09-04 18:34 873关于前缀查询PrefixQuery(前缀查询)。 准备工作就 ... -
Lucene-2.3.1 源代码阅读学习(30)
2009-09-04 18:34 1064关于Query的学习。 主要使用TermQuery和Bool ...
相关推荐
《Lucene-2.3.1 源代码阅读学习》 Lucene是Apache软件基金会的一个开放源码项目,它是一个高性能、全文本搜索库,为开发者提供了在Java应用程序中实现全文检索功能的基础架构。本篇文章将深入探讨Lucene 2.3.1版本...
总而言之,Lucene 2.3.1作为一款经典的搜索引擎框架,它的源代码不仅提供了学习信息检索理论的机会,也是实践和掌握Java编程、数据结构和算法的宝贵资源。通过对压缩包中的文件进行分析,开发者可以深入了解Lucene的...
通过深入学习和理解这些源代码文件,开发者可以更好地掌握 Lucene.Net 的核心功能,如索引构建、查询解析、搜索排序、分词和性能优化。这有助于在实际项目中实现高效、精确的全文搜索引擎。同时,研究源码也能提升对...
4.其中src文件夹内为全部源代码,WebRoot为web应用部署文件 5.本系统的最小有效组件集合为:(约定:以下“*.*”均表示目录下的所有单独文件,不包括文件夹,而“/s”则表示所有的文件夹及其内部内容) src\*.* /s ...
### Lucene+Solor知识点概述 #### 一、搜索引擎基础理论 **1.1 Google神话** - **起源与发展:** - Google成立于1998年,由Larry Page和Sergey Brin创立。 - 初期以PageRank算法为核心,有效解决了当时互联网...
- **开源协议**:使用Apache License 2.0协议,源代码完全开源,没有商业限制。 - **技术栈成熟**:使用当前最主流的J2EE开发框架和技术,易于学习和维护。 - **数据库支持广泛**:支持多种数据库,如MySQL、Oracle...
- **1.4.1 目录结构说明**:Solr项目的目录结构清晰,主要包括src/main/java下的源代码、src/main/resources下的资源文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr实例的工作目录,包含了索引数据、配置文件等...
- **1.4.1 目录结构说明**:Solr的核心源码主要由几个关键部分组成,如`src/main/java`包含Java源代码,`src/main/resources`存放配置文件等。 - **1.4.2 Solrhome说明**:Solrhome是Solr运行时使用的根目录,包含了...
CAS (Central Authentication Service) 是一种开放源代码的单点登录协议和服务实现,主要用于Web应用的安全身份验证。CAS支持跨域的身份验证管理,允许用户通过一个中心服务进行一次登录即可访问多个应用系统。 **...
2.3.1. 保存 ACL 数据确保持久性 2.3.2. 使用声明(Assert)来编写条件性的 ACL 规则 3. Zend_Auth 3.1. 简介 3.1.1. 适配器 3.1.2. 结果 3.1.3. 身份的持久(Persistence) 3.1.3.1. 在PHP Session 中的缺省...