Lucene 快速入门
2013-10-14 17:43
375 查看
Lucene使得全文索引这项技术活变得非常简单,我将用5分钟做个快速展示。
1.建立索引 这个例子展示的是在内存中为几个String建立索引
来看addDoc()方法:
2.查询 从标准输入读取查询信息,建立Lucene的Query对象
3.查找 利用2产生的Query对象建立Searcher对象在索引中查找
4.显示 显示查找结果
完整的Java代码:
1.建立索引 这个例子展示的是在内存中为几个String建立索引
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44); Directory index = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer); IndexWriter w = new IndexWriter(index, config); addDoc(w, "Lucene in Action", "193398817"); addDoc(w, "Lucene for Dummies", "55320055Z"); addDoc(w, "Managing Gigabytes", "55063554A"); addDoc(w, "The Art of Computer Science", "9900333X"); w.close();
来看addDoc()方法:
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { Document doc = new Document(); doc.add(new TextField("title", title, Field.Store.YES)); doc.add(new StringField("isbn", isbn, Field.Store.YES)); w.addDocument(doc); }
2.查询 从标准输入读取查询信息,建立Lucene的Query对象
String querystr = args.length > 0 ? args[0] : "lucene"; Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);
3.查找 利用2产生的Query对象建立Searcher对象在索引中查找
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("找到 " + 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("isbn") + "\t" + d.get("title")); }
完整的Java代码:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.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. analyzer
//
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
// 1. index
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, "Lucene in Action", "193398817");
addDoc(w, "Lucene for Dummies", "55320055Z");
addDoc(w, "Managing Gigabytes", "55063554A");
addDoc(w, "The Art of Computer Science", "9900333X");
w.close();
// 2. query
String querystr = args.length > 0 ? args[0] : "lucene";
Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);
// 3. search
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display
System.out.println("找到 " + 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("isbn") + "\t" + d.get("title")); }
reader.close();
}
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException { Document doc = new Document(); doc.add(new TextField("title", title, Field.Store.YES)); doc.add(new StringField("isbn", isbn, Field.Store.YES)); w.addDocument(doc); }
}
相关文章推荐
- Lucene2.0 快速入门
- Lucene Inverted index(倒排索引)原来快速入门
- 搜索引擎lucene超快速入门
- Lucene第一篇【介绍Lucene、快速入门】
- 全文检索Lucene(一)---快速入门
- Lucene第一篇【介绍Lucene、快速入门】
- Lucene2.0快速入门
- Lucene 快速入门
- Lucene 快速入门
- lucene快速入门---一个例子读懂
- lucene,你也会(7篇)——第一篇 快速入门
- 搜索引擎lucene超快速入门
- Lucene开发(一):快速入门
- 1.搜索引擎的历史,搜索引擎起步,发展,繁荣,搜索引擎的原理,搜索技术用途,信息检索过程,倒排索引,什么是Lucene,Lucene快速入门
- Lucene.NET使用入门(一)【实现快速搜索】
- 1.搜索引擎的历史,搜索引擎起步,发展,繁荣,搜索引擎的原理,搜索技术用途,信息检索过程,倒排索引,什么是Lucene,Lucene快速入门
- lucene快速入门
- lucene,你也会(15篇)——第一篇 快速入门
- Apache Lucene 快速入门指南
- lucene 快速入门