public abstract class BaseIndexingTestCase {
protected String[] keywords = {"1", "2"};
protected String[] unindexed = {"Netherlands", "Itely"};
protected String[] unstored = {"Amsterdam has lots of bridges", "Venice has lots of canals"};
protected String[] text = {"Amsterdam", "Venice"};
protected Directory dir;
public void init() throws IOException {
String indexDir = "D:/test";
dir = FSDirectory.open(new File(indexDir));
}
public BaseIndexingTestCase() {
try {
init();
} catch (IOException e) {
e.printStackTrace();
}
}
//===================创建文档=====================
protected void setUp() throws IOException {
addDocuments(dir);
}
protected void addDocuments(Directory dir2) throws CorruptIndexException, LockObtainFailedException, IOException {
IndexWriter writer = getIndexWriter(dir2);
for (int i = 0; i < keywords.length; i++) {
Document doc = new Document();
doc.add(new Field("id",keywords[i], Store.YES, Index.ANALYZED));
doc.add(new Field("city",text[i], Store.NO, Index.ANALYZED));
doc.add(new Field("contents",unstored[i], Store.YES, Index.ANALYZED));
doc.add(new Field("unindexed",keywords[i], Store.YES, Index.NO));
writer.addDocument(doc);
}
writer.close();
}
//===============================搜索=======================
protected void search(String field, String value) throws CorruptIndexException, IOException, ParseException {
IndexSearcher search = getIndexSearcher();
QueryParser parser = new QueryParser(Version.LUCENE_36, field, getAnalyzer());
Query query = parser.parse(value);
TopDocs topDocs = search.search(query, 10);
ScoreDoc[] docs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : docs) {
int docID = scoreDoc.doc;
Document document = search.doc(docID);
List<Fieldable> list = document.getFields();
for (Fieldable fieldable : list) {
System.out.println(fieldable.name() + " : " + fieldable.stringValue());
}
}
search.close();
}
//=============================删除文档=========================
protected void delete(Term term) throws CorruptIndexException, IOException {
IndexWriter writer = getIndexWriter(dir);
writer.deleteDocuments(term);
//老版本中,通过IndexReader删除,且在IndexReader关闭时提交
writer.forceMergeDeletes();
writer.close();
}
protected void delete(Query query) throws CorruptIndexException, IOException {
IndexWriter writer = getIndexWriter(dir);
writer.deleteDocuments(query);
writer.forceMergeDeletes();
writer.close();
}
protected void delete(String name, String value) throws CorruptIndexException, IOException, ParseException {
Query query = getQuery(name, value);
IndexWriter writer = getIndexWriter(dir);
writer.deleteDocuments(query);
writer.forceMergeDeletes();
writer.close();
}
protected void deleteAll() throws ParseException, CorruptIndexException, IOException {
Query query = getQuery("*","*");
delete(query);
}
//=======================更新索引=====================
protected void update(String field, String value, Document doc) throws CorruptIndexException, LockObtainFailedException, IOException {
IndexWriter writer = getIndexWriter(dir);
Term term = new Term(field, value);
//先删除原来的索引,后添加新的索引
writer.updateDocument(term, doc);
}
//======================恢复删除================================
protected void undelete() throws CorruptIndexException, IOException {
//老版本通过IndexReader的undeleteAll恢复(在执行物理删除前才行)
}
//========================命中数===================
protected int getHitCount(String field, String value) throws IOException, ParseException {
IndexSearcher search = getIndexSearcher();
Query query = getQuery(field, value);
TopDocs topDocs = search.search(query,10);
return topDocs.totalHits;
}
//========================索引中总文档数(包括删除未提交的文档)======================
protected int maxDocs() throws CorruptIndexException, LockObtainFailedException, IOException {
IndexWriter writer = getIndexWriter(dir);
IndexReader reader = getIndexSearcher().getIndexReader();
System.out.println(writer.maxDoc() + "-" + reader.maxDoc());
writer.close();
reader.close();
return 0;
}
//=======================索引中可用的文档数(不包括删除未提交的文档)=====================
protected int numDocs() throws CorruptIndexException, LockObtainFailedException, IOException {
IndexWriter writer = getIndexWriter(dir);
IndexReader reader = getIndexSearcher().getIndexReader();
System.out.println(writer.numDocs() + "-" + reader.numDocs());
writer.close();
reader.close();
return 0;
}
//=================================================================
private Query getQuery(String name, String value) throws ParseException {
QueryParser parser = new QueryParser(Version.LUCENE_36, name, getAnalyzer());
Query query = parser.parse(value);
return query;
}
private IndexSearcher getIndexSearcher() throws CorruptIndexException, IOException {
IndexReader reader = IndexReader.open(dir);
IndexSearcher search = new IndexSearcher(reader);
return search;
}
private IndexWriter getIndexWriter(Directory dir2) throws CorruptIndexException, LockObtainFailedException, IOException {
IndexWriter writer = new IndexWriter(dir2, getIndexWriterConfig());
return writer;
}
private IndexWriterConfig getIndexWriterConfig() {
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, getAnalyzer());
LogMergePolicy policy = new LogDocMergePolicy();
policy.setUseCompoundFile(true);
config.setMergePolicy(policy);
return config;
}
private Analyzer getAnalyzer() {
return new SimpleAnalyzer(Version.LUCENE_36);
}
}