lucene3.0.0 indexReader 和indexSearch 区别 索引的删除 索引的优化

lucene3.0.0 indexReader 和indexSearch  区别 索引的删除  索引的优化
package com.txt.test2;

//indexReader  和 indexSearch  区别
import java.io.File;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
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.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class IndexReaderTest {
String txt1 = "holle world chengdu";
String txt2 = "holle world sichuang chengdu";
String txt3 = "holle world sichuang chengdu chengdu";
String txt4 = "holle world sichuang chengdu chengdu chengdu";

private File file = new File("f:" + File.separator + "indexDir8");
// 创建词元分析器
private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

@Test
public void create() throws Exception {
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);

// 创建索引器  追加的方式
IndexWriter writer = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);

// 创建文档
/**
* 参数1 : 字段名称 相当于数据库的字段名称 参数2 :数据源名称 参数3 : 是否要把数据源名称存储到索引库中Store.YES 参数4
* : 是否要使用分词器作分析Index.ANALYZED
*/
Document document = new Document();
document.add(new Field("id", "1", Store.YES, Index.ANALYZED));
document.add(new Field("content", txt1, Store.YES, Index.ANALYZED));
Document document2 = new Document();
document2.add(new Field("id", "2", Store.YES, Index.ANALYZED));
document2.add(new Field("content", txt2, Store.YES, Index.ANALYZED));
Document document3 = new Document();
document3.add(new Field("id", "3", Store.YES, Index.ANALYZED));
document3.add(new Field("content", txt3, Store.YES, Index.ANALYZED));
Document document4 = new Document();
document4.add(new Field("id", "4", Store.YES, Index.ANALYZED));
document4.add(new Field("content", txt4, Store.YES, Index.ANALYZED));

writer.addDocument(document);
writer.addDocument(document2);
writer.addDocument(document3);
writer.addDocument(document4);

writer.close();
}

//从索引中删除Document对象  已删除Query对象的形式
@Test
public void deleteQuery()throws Exception{
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);

// 创建索引器  追加的方式
IndexWriter writer = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);
// 创建查询解析器  以id为查询目标
QueryParser parser = new QueryParser(Version.LUCENE_30, "id", analyzer);
Query query = parser.parse("1");
//删除document  传一个query对象进去
//相当于  delete from tanbieName where id = 1;
writer.deleteDocuments(query);
writer.close();
}
//删除  传一个Term对象
@Test
public void deleteTerm()throws Exception{
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);

// 创建索引器  追加的方式
IndexWriter writer = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);
//封装了一个field的字段名 和 它对应的那个值
Term term = new Term("id", "2");
writer.deleteDocuments(term);
writer.close();
}
//优化索引   可以将你索引目录下的一些垃圾的文件 清理掉
@Test
public void optimize() throws Exception{
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);

// 创建索引器  追加的方式
IndexWriter writer = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);
//优化索引
writer.optimize();
writer.close();

}
//修改索引  先删除索引  再添加索引
@Test
public void updateIdx() throws Exception{
Document document4 = new Document();
document4.add(new Field("id", "5", Store.YES, Index.ANALYZED));
document4.add(new Field("content", "holle content4", Store.YES, Index.ANALYZED));
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);

// 创建索引器  追加的方式
IndexWriter writer = new IndexWriter(directory, analyzer,
MaxFieldLength.LIMITED);
//条件
Term term = new Term("id", "4");
//更新  update tableName set content = content4 where id = 4;
writer.updateDocument(term, document4);
writer.close();

}
@Test
public void search() throws Exception {
// 索引存储目录
Directory directory = new SimpleFSDirectory(file);
// 创建查询器
IndexSearcher searcher = new IndexSearcher(directory, true);
// 创建查询解析器 对文档document的content字段进行查询
QueryParser parser = new QueryParser(Version.LUCENE_30, "content",
new StandardAnalyzer(Version.LUCENE_30));
//创建查询类
String key = "holle";
Query query = parser.parse(key);
//获取钱n位索引
TopDocs tDocs = searcher.search(query, 100);
for(int i = 0; i<tDocs.scoreDocs.length; i++){
ScoreDoc sDoc = tDocs.scoreDocs[i];

Document document = searcher.doc(sDoc.doc);
System.out.println("id:"+document.get("id"));
System.out.println("content:"+document.get("content"));

}
searcher.close();
}
//所有的内容都可以查出来  包括已经被删除的索引内容也会查出来
@Test
public void reader() throws Exception{
// 索引库存储目录
Directory directory = new SimpleFSDirectory(file);
//直接读取索引库目录
IndexReader reader = IndexReader.open(directory,true);
//获取索引目录有多少条索引文档
int max =  reader.maxDoc();
for (int i = 0; i < max; i++) {
Document document = reader.document(i);
System.out.println("id : "+document.get("id"));
System.out.println("content : "+document.get("content"));
}
}
}

 

 

你可能感兴趣的:(Lucene)