索引的读取工具IndexReader

//索引的读取工具IndexReader
//在Lucene中的index包中有一个重要的工具IndexReader。
//它主要负责对索引的各种读取和维护工作。
//IndexReader本身是一个抽象类。它有一个子类MultiReader,
//通常,无须关注其子类的实现,因为IndexReader本身提供了一个静态方法
//来获取一个它的子类的实现
package index;


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.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;


public class IndexWriter {

IndexReader reader = IndexReader.open(INDEX_STORE_PATH);

//创建索引
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);

Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();

Field f1 = new Field("bookname", "钢铁是怎么样炼成的", Field.Store.YES, Field.Index.TOKENIZED);
Field f2 = new Field("bookname", "英雄儿女", Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field("bookname", "篱笆女人和狗", Field.Store.YES, Field.Index.TOKENIZED);
doc1.add(f1);
doc2.add(f2);
doc3.add(f3);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();

//使用IndexReader来读取索引
IndexReader reader= IndexReader.open(INDEX_STORE_PATH);

//显示索引内所有的Document
System.out.println("索引文档列表: ");
for(int i = 0; i < reader.numDocs(); i++){
System.out.println(reader.document(i));
}

//输出当前索引的版本信息
System.out.println("版本: " + reader.getVersion());

//输出当前索引的文档数量
System.out.println("索内的文档数量: " + reader.numDocs());

//构造一个词条并在索引中查找
System.out.println();
System.out.println("查找词条 '女' ");
Term term1 = new Term("bookname", "女");
TermDocs docs = reader.termDocs(term1);
while(docs.next()){
System.out.println("---------------------");
System.out.println("含有所有查找的<" + term1 + ">的Docuemnt的编号为" + docs.doc());
System.out.println("Term在文档中的出现次数" + docs.freq());
System.out.println("---------------------");
}

reader.close();

//在此向索引目录中添加一个文档
IndexWriter writer2 = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), false);
writer2.serUseCompoundFile(false);
Document doc4 = new Document();
Field f4 = new Field("bookname", "钢 铁 战 士", Field.Store.YES, Field.Index.TOKENIZED);
doc4.add(f4);
writer2.addDocument(doc4);
writer2.close();

System.out.println("==============================================");

//使用IndexReader来读取索引
IndexReader reader2 = new IndexReader.open(INDEX_STORE_PATH);

//显示索引中的所有文件
for(int i = 0; i < reader2.numDocs(); i++){
System.out.println(reader2.document(i));
}

//输出当前索引的版本信息
System.out.println("版本" + reader2.getVersion());

//输出当前索引的文档数量
System.out.println("索内的文的数量" + reader2.numDocs());

System.out.println("查找词条'女'");
Term term2 = new Term("bookname", "女");
docs = reader.termDocs(term2);
while(docs.next()){
System.out.println("------------------------");
System.out.println("含有所查找的<" + term2 + ">的Document的编号为 " + docs.doc());
System.out.println("Term在文档中出现的次数" + docs.freq());
System.out.println("------------------------");
}

System.out.println();
System.out.println("查找词条'钢'");
Term term3 = new Term("bookname","钢");
docs = reader2.termDocs(term3);
while(docs.next()){
System.out.println("------------------------");
System.out.println("含有所查找的<" + term3 + ">的Document的编号为 " + docs.doc());
System.out.println("Term在文档中出现的次数" + docs.freq());
System.out.println("------------------------");
}
}

你可能感兴趣的:(索引的读取工具IndexReader)