Lucene (1)

Submitted by Lizhe on Wed, 12/20/2017 - 15:50

在Lucene中, 要经过如下步骤才能获得检索结果

1. 创建分析器

StandardAnalyzer analyzer = new StandardAnalyzer();

2. 将原始内容转换成"文档"

Directory index = new RAMDirectory();

3. 向索引添加文档

IndexWriterConfig config = new IndexWriterConfig(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();

4. 调用查询

String querystr = args.length > 0 ? args[0] : "lucene";

Query q = new QueryParser("title", analyzer).parse(querystr);

IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
searcher.search(q, collector);
   

5. 获得结果

ScoreDoc[] hits = collector.topDocs().scoreDocs;

package lzlucene;


import java.io.IOException;

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;

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();
 
    // 1. create the index
    Directory index = new RAMDirectory();
 
    IndexWriterConfig config = new IndexWriterConfig(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";
 
    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query q = new QueryParser("title", analyzer).parse(querystr);
 
    // 3. search
    int hitsPerPage = 10;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
    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("isbn") + "\t" + d.get("title"));
    }
 
    // reader can only be closed when there
    // is no need to access the documents any more.
    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));
 
    // use a string field for isbn because we don't want it tokenized
    doc.add(new StringField("isbn", isbn, Field.Store.YES));
    w.addDocument(doc);
  }
}

 

执行简单的索引过程需要用到以下几个类

1. IndexWriter

IndexWriter是索引过程的核心组件.

这个类负责创建新索引或者打开已有索引,以及向索引中添加,删除或是更新被索引文档的信息.

IndexWriter需要开辟一定空间来存储索引, 该功能可以由Directory完成.

2. Directory

Directory类描述Lucenen索引的存放位置.

3. Analyzer

文本文件在被索引之前,需要经过Analyzer(分析器)处理.

4. Document

Document对象代表一些字段(Field)的集合.

5. Field

 

搜索过程中需要以下核心类

1. IndexSearcher

IndexSearcher用于搜索由IndexWriter类创建的索引

2. Term

Term对象是搜索功能的基本单元.与Field对象类似, Term对象包含一对字符串元素: 域名和单词

以下两种写法在简单应用上是等价的

Query q = new QueryParser("title", analyzer).parse(querystr);
Query q = new TermQuery(new Term("title","lucene"));

3. Query

Lucene含有许多具体的Query子类

比如上面的TermQuery

4. TopDocs

Topdocs

Topdocs类是一个简单的指针容器, 指针一般指向前N个排名的搜索结果