1.TermQuery:
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.search.TermQuery;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
TermQuerySearcher {
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
String q = "ERROR"
;
Term t = new
Term("contents"
, q.toLowerCase());
Query query = new
TermQuery(t);
// QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
// Query query = parser.parse(q);
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
2.RangeQuery:
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.search.RangeQuery;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
RangeQuerySearcher {
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
String b = "2007"
;
String e = "2008"
;
Term begin = new
Term("contents"
, b);
Term end = new
Term("contents"
, e);
RangeQuery query = new
RangeQuery(begin, end, true
);
// QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
// Query query = parser.parse("[2007 TO 2008]");
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
3.BooleanQuery:
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.BooleanClause;
import
org.apache.lucene
.search.BooleanQuery;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.search.RangeQuery;
import
org.apache.lucene
.search.TermQuery;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
BooleanQuerySearcher {
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
Query tq = new
TermQuery(new
Term("contents"
, "ERROR"
.toLowerCase()));
RangeQuery rq = new
RangeQuery(new
Term("contents"
, "2007"
), new
Term("contents"
, "2008"
), true
);
BooleanQuery query = new
BooleanQuery();
query.add(tq, BooleanClause.Occur.MUST);
query.add(rq, BooleanClause.Occur.MUST);
// QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
// Query query = parser.parse("error AND [2007 TO 2008]");
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
4.PrefixQuery PrefixQuery前缀搜索
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.PrefixQuery;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
PrefixQuerySearcher {
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
String q = "20"
;
Term t = new
Term("contents"
, q.toLowerCase());
PrefixQuery query = new
PrefixQuery(t);
// QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
// Query query = parser.parse(q + "*");
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
5.PhraseQuery PhraseQuery短语搜索
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.PhraseQuery;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
PhraseQuerySearcher {
public
static
void
main(String[] args) throws
IOException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
String[] phrase = {"ERROR"
.toLowerCase(), "TEST"
.toLowerCase()};
PhraseQuery query = new
PhraseQuery();
query.setSlop(1
);
for
(int
i = 0
; i < phrase.length; i++) {
query.add(new
Term("contents"
, phrase));
}
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
6.WildcardQuery
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.search.WildcardQuery;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
public
class
WildcardQuerySearcher {
//通配符查询
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
Query query = new
WildcardQuery(new
Term("contents"
, "?ER*"
.toLowerCase()));
//====================================================================================
// QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
// Query query = parser.parse("?ER*".toLowerCase());
//====================================================================================
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
}
7.FuzzyQuery
Java代码
package
com.lucene
.search;
import
java.io.File;
import
java.io.IOException;
import
org.apache.lucene
.analysis.standard.StandardAnalyzer;
import
org.apache.lucene
.index
.Term;
import
org.apache.lucene
.queryParser.ParseException;
import
org.apache.lucene
.queryParser.QueryParser;
import
org.apache.lucene
.search.FuzzyQuery;
import
org.apache.lucene
.search.Hits;
import
org.apache.lucene
.search.IndexSearcher;
import
org.apache.lucene
.search.Query;
import
org.apache.lucene
.store
.Directory;
import
org.apache.lucene
.store
.FSDirectory;
//模糊查询查出相近的词
public
class
FuzzyQuerySearcher {
public
static
void
main(String[] args) throws
IOException, ParseException {
File indexDir = new
File("C:\\test\\
index
"
);
Directory fsDir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new
IndexSearcher(fsDir);
Query query = new
FuzzyQuery(new
Term("contents"
, "XERRORX"
.toLowerCase()));
//QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
//Query query = parser.parse("ERROR~".toLowerCase());
Hits hits = searcher.search(query);
System.out.println("共有"
+ searcher.maxDoc() + "条索引,命中"
+ hits.length() + "条"
);
for
(int
i=0
; i<hits.length(); i++) {
int
DocId = hits.id(i);
String DocPath = hits.doc(i).get("filename"
);
System.out.println(DocId + ":"
+ DocPath);
}
}
分享到:
相关推荐
【标题】:“Lucene3 实例解析” 在深入探讨Lucene3这个主题之前,我们先来了解一下Lucene是什么。Lucene是一个开源的全文检索库,由Apache软件基金会开发,广泛应用于各种信息检索系统中,包括搜索引擎、内容管理...
**Lucene 全文搜索引擎实例:Java Lucene 实例** Lucene 是 Apache 软件基金会的一个开源项目,它提供了一个高性能、可扩展的信息检索库。这个实例将深入讲解如何在 Java 中使用 Lucene 来创建索引并执行各种搜索...
lucenetest.rar,lucene,全文检索,lucene例子 lucenetest.rar,lucene,全文检索,lucene例子lucenetest.rar,lucene,全文检索,lucene例子
二、Lucene实例操作流程 1. 创建索引(Indexing):首先,我们需要创建一个索引Writer,然后添加文档到索引中。每个文档包含多个字段,每个字段都有其特定的属性,如是否可搜索、是否存储原始内容等。 ```java ...
【Lucene.NET 实例】 Lucene.NET 是一个开源全文搜索引擎库,它是 Apache Lucene 的 .NET 版本,为 .NET 开发者提供了强大的搜索功能。Lucene.NET 提供了高性能、可扩展的文本搜索解决方案,广泛应用于各种应用程序...
lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子lucene quartz 例子
总的来说,通过这段代码实例,我们可以了解到使用Lucene进行搜索的基本步骤,包括创建查询对象、初始化`IndexSearcher`、执行查询以及处理搜索结果。掌握这些基本操作,对于理解和应用Lucene进行全文检索至关重要。
在这个"ssh+lucene搜索实例"中,我们可以理解为结合了SSH和Lucene两个技术,以实现远程服务器上的全文检索功能。例如,可能有一个需求是在多个远程服务器上存储大量数据,而这些数据需要通过关键词进行快速搜索。在...
**Lucene索引器实例详解** Lucene是一个高性能、全文本搜索库,由Apache软件基金会开发,被广泛应用于各种搜索引擎的构建。它提供了一个高级的、灵活的、可扩展的接口,使得开发者能够轻松地在应用程序中实现全文...
**Lucene 搜索实例** Apache Lucene 是一个高性能、全文本搜索引擎库,它为开发者提供了在各种应用程序中实现全文检索的工具集。Lucene 并不是一个完整的应用,而是一个 Java 类库,可以被其他 Java 应用程序所使用...
从给定的文件信息中,我们可以提取出关于Apache Lucene的基本使用和实例的详细知识点,以下是对这些知识点的深入解析: ### Lucene简介 Apache Lucene是一个高性能、全功能的文本搜索引擎库,由Java编写,提供了对...
在“lucene入门小例子”中,可能包含创建索引、添加文档、查询和显示结果等基本操作的示例代码。这些代码将帮助你了解如何在实际项目中集成和使用Lucene,进一步熟悉全文检索的原理和实践。 在学习过程中,你可能会...
通过添加多个TermQuery或其他Query子类实例到BooleanQuery来构建复杂的逻辑查询。 举例来说,以下是如何创建一个"星期一"和"五月一日"的逻辑组合查询: ```java Term term1 = new Term("content", "星期一"); Term ...
以上就是关于"Lucene4.7-Web例子"的详细解析,通过这个示例,开发者可以了解到如何在实际的Web环境中结合SpringMVC和MyBatis3使用Lucene4.7构建高效搜索引擎。实践中,可以参考提供的"gusao.sql"数据库脚本,"请读我...
lucene实例是一个比较详细的例子,包括lucene的入门到高级实例,代码里有比较详细的实例,所有的实例都是通过junit来测试的。实例包括各种搜索:如通配符查询、模糊查询、查询结果的分页、中文分词器、自定义分词器...
lucene3.0 例子lucene3.0 例子 lucene3.0 例子 ,很好的学习,只有原代原,jar 包自己加上去就OK了
经典的Lucene实例代码及详细解析以及Lucene结构流程介绍 Lucene是一个功能强大且灵活的开源搜索引擎库,它提供了一个简单易用的API,允许开发者快速构建搜索应用程序。下面将对Lucene的实例代码和结构流程进行详细...