先看下面的代码:
- package demo.basic;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.IndexWriter;
- 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.TopDocs;
- import org.apache.lucene.store.RAMDirectory;
- public class LuceneTest {
- public static void main(String[] args) throws Exception {
- RAMDirectory directory = new RAMDirectory();
- IndexWriter.MaxFieldLength maxFieldLength = new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH);
- IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true, maxFieldLength);
- Document doc = new Document();
- doc.add(new Field("title", "php book", Field.Store.YES, Field.Index.ANALYZED));
- doc.add(new Field("content", "you can use this book to learn php.", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(doc);
- doc = new Document();
- doc.add(new Field("title", "java book", Field.Store.YES, Field.Index.ANALYZED));
- doc.add(new Field("content", "this is the best book for learn java.", Field.Store.YES, Field.Index.ANALYZED));
- writer.addDocument(doc);
- writer.close();
- IndexSearcher searcher = new IndexSearcher(directory);
- QueryParser parser = new QueryParser("title", new StandardAnalyzer());
- Query query = parser.parse("book");
- TopDocs topDocs = searcher.search(query, 100);
- ScoreDoc[] hits = topDocs.scoreDocs;
- System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length + "条");
- for (int i = 0; i < hits.length; i++) {
- int DocId = hits[i].doc;
- Document document = searcher.doc(DocId);
- System.out.println(DocId + ":" + document.get("content"));
- }
- searcher.close();
- }
- }
输出结果:
共有2条索引,命中2条
0:you can use this book to learn php.
1:this is the best book for learn java.
分析器StandardAnalyzer是lucene自带的。
上例中尝试了把2组数据放入索引中,每组数据有2个字段。这种结构和数据库表结构是非常相似的,使用时可以进行联想。
Field.Store.YES 表示:建立索引时该字段会被存储起来,待查询时可以通过 document.get 方法取出。
API说明:http://lucene.apache.org/java/2_4_0/api/core/index.html
Field.Index.ANALYZED 表示:建立索引时该字段会被指定的分析器进行分析。
API说明:http://lucene.apache.org/java/2_4_0/api/core/index.html