package org.lucene.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 {
String[] ids = { "aa", "bb", "cc", "dd", "ee", "ff", "gg" };
String[] emails = { "aaa@lucenes.com", "bbb@lucenes.com",
"ccc@lucenes.com", "ddd@lucenes.com", "eee@lucenes.com",
"fff@lucenes.com", "ggg@lucenes.com" };
String[] contents = { "aaaaaaaaaaaaaa", "bbbbbbbbbbbbbb",
"ccccccccccccccc", "dddddddddddddd", "eeeeeeeeeeeeee",
"fffffffffffffff", "gggggggggggggg" };
String[] names = { "zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi",
"zhaoba", "chenjiu" };
Integer[] attachs = { 1, 2, 5, 4, 6, 2, 3 };
Directory directory = null;
public IndexUtil() {
try {
directory = FSDirectory.open(new File(
"E:/luceneworkspace/Lucene2/path"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.deleteAll();
Document document = null;
for (int i = 0; i < ids.length; i++) {
document = new Document();
document.add(new Field("id", ids[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
document.add(new Field("email", emails[i], Field.Store.YES,
Field.Index.NOT_ANALYZED));
document.add(new Field("content", contents[i], Field.Store.NO,
Field.Index.ANALYZED));
document.add(new Field("name", names[i], Field.Store.YES,
Field.Index.NOT_ANALYZED));
indexWriter.addDocument(document);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除索引
*/
public void delete() {
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.deleteDocuments(new Term("id", "aa"));
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 恢复索引
*/
public void undelete() {
IndexReader indexReader = null;
try {
indexReader = IndexReader.open(directory, false);
indexReader.undeleteAll();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexReader != null)
indexReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 清空回收站索引
*/
public void clear(){
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.forceMergeDeletes();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 更新索引
*/
public void update(){
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc=new Document();
doc.add(new Field("id", "hh", Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email", emails[2], Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("content", contents[3], Field.Store.NO,
Field.Index.ANALYZED));
doc.add(new Field("name", names[4], Field.Store.YES,
Field.Index.NOT_ANALYZED));
indexWriter.updateDocument(new Term("id","aa"), doc);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void query() {
try {
IndexReader indexReader = IndexReader.open(directory);
System.out.println("maxDoc:" + indexReader.maxDoc());
System.out.println("numDocs:" + indexReader.numDocs());
System.out.println("numDeleteDocs:"+indexReader.numDeletedDocs());
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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 {
String[] ids = { "aa", "bb", "cc", "dd", "ee", "ff", "gg" };
String[] emails = { "aaa@lucenes.com", "bbb@lucenes.com",
"ccc@lucenes.com", "ddd@lucenes.com", "eee@lucenes.com",
"fff@lucenes.com", "ggg@lucenes.com" };
String[] contents = { "aaaaaaaaaaaaaa", "bbbbbbbbbbbbbb",
"ccccccccccccccc", "dddddddddddddd", "eeeeeeeeeeeeee",
"fffffffffffffff", "gggggggggggggg" };
String[] names = { "zhangsan", "lisi", "wangwu", "zhaoliu", "tianqi",
"zhaoba", "chenjiu" };
Integer[] attachs = { 1, 2, 5, 4, 6, 2, 3 };
Directory directory = null;
public IndexUtil() {
try {
directory = FSDirectory.open(new File(
"E:/luceneworkspace/Lucene2/path"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.deleteAll();
Document document = null;
for (int i = 0; i < ids.length; i++) {
document = new Document();
document.add(new Field("id", ids[i], Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
document.add(new Field("email", emails[i], Field.Store.YES,
Field.Index.NOT_ANALYZED));
document.add(new Field("content", contents[i], Field.Store.NO,
Field.Index.ANALYZED));
document.add(new Field("name", names[i], Field.Store.YES,
Field.Index.NOT_ANALYZED));
indexWriter.addDocument(document);
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除索引
*/
public void delete() {
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.deleteDocuments(new Term("id", "aa"));
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 恢复索引
*/
public void undelete() {
IndexReader indexReader = null;
try {
indexReader = IndexReader.open(directory, false);
indexReader.undeleteAll();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexReader != null)
indexReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 清空回收站索引
*/
public void clear(){
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
indexWriter.forceMergeDeletes();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 更新索引
*/
public void update(){
IndexWriter indexWriter = null;
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(
Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document doc=new Document();
doc.add(new Field("id", "hh", Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("email", emails[2], Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("content", contents[3], Field.Store.NO,
Field.Index.ANALYZED));
doc.add(new Field("name", names[4], Field.Store.YES,
Field.Index.NOT_ANALYZED));
indexWriter.updateDocument(new Term("id","aa"), doc);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null)
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void query() {
try {
IndexReader indexReader = IndexReader.open(directory);
System.out.println("maxDoc:" + indexReader.maxDoc());
System.out.println("numDocs:" + indexReader.numDocs());
System.out.println("numDeleteDocs:"+indexReader.numDeletedDocs());
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
################################