5分钟学用Lucene
Lucene很容易使你的应用程序添加文本搜索的功能,实际上,非常容易,我将在5分钟内向您展示!
( 译者注: 实际上,在此之前需要理解搜索引擎的工作原理,和Lucene的基本概念 )
1. 索引.
为了这个简单的例子,我们将建立一个存储在内存中的一些字符串的索引。
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "The Art of Computer Science");
w.close();
2. 查询.
读取从标准输入(stdin)输入的查询,解析,并从中建立lucence的查询.
String querystr = args.length > 0 ? args[0] : "lucene";
Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(querystr);
3. 搜索.
通过使用Query来创建一个Searcher来搜索索引, 然后实例化一个 TopScoreDocCollector 来收集前10个Hits (译者注: 查询结果)
int hitsPerPage = 10;
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
4. 显示.
现在我们已经得到了搜索的结果,显示出来即可.
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}
完整的代码如下:
(译者注: 在最新的Lucene 3.6版本上面做了修正 )
package hellolucene;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import java.io.IOException;
public class HelloLucene {
public static void main(String[] args) throws IOException, ParseException {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
// 1. create the index
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "The Art of Computer Science");
w.close();
// 2. query
String querystr = args.length > 0 ? args[0] : "lucene";
// the "title" arg specifies the default field to use
// when no field is explicitly specified in the query.
Query q = new QueryParser(Version.LUCENE_36, "title", analyzer).parse(querystr);
// 3. search
int hitsPerPage = 10;
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}
// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}
private static void addDoc(IndexWriter w, String value) throws IOException {
Document doc = new Document();
doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
输出结果:
Found 2 hits.
1. Lucene in Action
2. Lucene for Dummies
参考原文: http://www.lucenetutorial.com/lucene-in-5-minutes.html
分享到:
相关推荐
Lucene的基础知识 1、案例分析:什么是全文检索,如何实现全文检索 2、Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3、配置开发环境 4、创建索引库 5、查询索引库 6、分析器的分析过程 a) 测试分析器的分词...
NULL 博文链接:https://iamyida.iteye.com/blog/2199848
NULL 博文链接:https://iamyida.iteye.com/blog/2202111
NULL 博文链接:https://iamyida.iteye.com/blog/2205114
NULL 博文链接:https://iamyida.iteye.com/blog/2197839
NULL 博文链接:https://iamyida.iteye.com/blog/2202651
NULL 博文链接:https://iamyida.iteye.com/blog/2203743
NULL 博文链接:https://iamyida.iteye.com/blog/2203575
NULL 博文链接:https://iamyida.iteye.com/blog/2207080
NULL 博文链接:https://iamyida.iteye.com/blog/2201372
NULL 博文链接:https://iamyida.iteye.com/blog/2206107
NULL 博文链接:https://iamyida.iteye.com/blog/2193345
NULL 博文链接:https://iamyida.iteye.com/blog/2199368
一步一步跟我学习lucene是对近期做lucene索引的总结,
Lucene5.2.1 入门学习例子. 这是别人的例子源码。可以参考。内有使用说明。
NULL 博文链接:https://iamyida.iteye.com/blog/2204455
NULL 博文链接:https://iamyida.iteye.com/blog/2201291
文档中包含Lucene4.0.0版本jar包,中文分词器jar包,Lucene实例代码 1:建立索引 2:各种搜索方式方法 3:删除索引 4:检查索引文件 5:恢复删除的索引 6:强制删除 7:更新索引 8:合并索引 9:高亮回显 供大家参考...
NULL 博文链接:https://iamyida.iteye.com/blog/2196855
1.16 Lucene学习总结之七:Lucene搜索过程解析(5) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 1.17 Lucene学习总结之七:Lucene搜索过程解析(6) . . . . . . . . . . . . ....