还是那两个java文件
IndexUtil.java
Reader就是一根输入的管道,Writer就是一个输出的管道
IndexWriter.deleteDocument()//可以删除Query也可以删除Term
IndexReader.undeleteAll()
IndexWriter.forceMergeDelere()
IdexWriter.updateDocument()
通过IndexReader.numDocs()、maxDoc()、numDeletedDocs()查看删除情况
package org.itat.index;
//创建索引
import java.io.File;
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.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
public class IndexUtil {
private String[] ids={"1","2","3","4","5","6"};
private String[] emails={"aa@aa.org","bb@bb.org","cc@cc.org","dd@@dd.org","ee@ee.org","ff@ff.org"};
private String[] contents={"helloaa","helloboy","hellocc","hellodog","helloegg","hellofox"};
private int[] attachs={1,2,3,4,5,6};
private String[] names={"zhangsan","lisi","wangwu","zhaoliu","wuba","chenjiu"};
private Directory directory=null;
public IndexUtil()
{
try {
directory=FSDirectory.open(new File("e:/lucene/index02"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void query()
{
try {
IndexReader reader=IndexReader.open(directory);
System.out.println("numdocs:"+reader.numDocs());
System.out.println("maxDocs:"+reader.maxDoc());
System.out.println("detelemaxDocs:"+reader.numDeletedDocs());
reader.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void index()
{
IndexWriter writer=null;
Document doc=null;
try {
// writer.deleteAll();
writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
for(int i=0;i<ids.length;i++)
{
doc=new Document();
doc.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("emails",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("contents",contents[i],Field.Store.NO,Field.Index.ANALYZED));
//谷歌搜索前面那个简介就是权值
doc.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(writer!=null)
{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//删除
public void delete()
{
IndexWriter writer=null;
try {
writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
//参数是一个选项.可以是一个query,也可以是一个term term就是一个精确查找的值
//此时删除的文档并未完全删除,而是存储在回收站中,可以恢复的
writer.deleteDocuments(new Term("id","1"));
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (writer!=null) {
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//恢复
public void undelete()
{
try {
//恢复时必须把reader的只读设为false
IndexReader reader=IndexReader.open(directory,false);
reader.undeleteAll();
reader.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//清空回收站,强制优化
public void forceDelete()
{
IndexWriter writer=null;
try {
writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
writer.forceMergeDeletes();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (writer!=null) {
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void merge()
{
IndexWriter writer=null;
try {
writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
//将索引合并为两段,建议不实用,开销太大
writer.forceMerge(2);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (writer!=null) {
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//更新索引
public void update()
{
// lucene本身不支持更新
// 通过删除索引然后再建立索引来更新
IndexWriter writer=null;
Document doc=null;
try {
writer =new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_35,
new StandardAnalyzer(Version.LUCENE_35)));
doc=new Document();
doc.add(new Field("id","111",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("emails",emails[0],Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("contents",contents[0],Field.Store.NO,Field.Index.ANALYZED));
doc.add(new Field("name",names[0],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
writer.updateDocument(new Term("id","1"), doc);
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(writer!=null)
{
try {
writer.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
测试类
package org.itat.test;
import org.itat.index.IndexUtil;
import org.junit.Test;
public class TestIndex {
@Test
public void testIndex()
{
IndexUtil index=new IndexUtil();
index.index();
}
@Test
public void testQuery()
{
IndexUtil index=new IndexUtil();
index.query();
}
@Test
public void testDelete()
{
IndexUtil index=new IndexUtil();
index.delete();
}
@Test
public void testUnDelete()
{
IndexUtil index=new IndexUtil();
index.undelete();
}
@Test
public void testForceDelete()
{
IndexUtil index=new IndexUtil();
index.forceDelete();
}
@Test
public void testUpdate()
{
IndexUtil index=new IndexUtil();
index.update();
}
}