public static void main(String[] args) throws CorruptIndexException,
LockObtainFailedException, IOException, ParseException {
CreateIndex2 index = new CreateIndex2();
index.createIndex();
index.search();
}
private void createIndex() throws CorruptIndexException, LockObtainFailedException, IOException{
IndexWriter writer = new IndexWriter(PATH,getAnalyzer(), true);
int i = 0;
while (i++ < 10) {
Document doc = new Document();
Document doc2 = new Document();
doc.add(new Field("content", "上海", Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("content", "泸", Field.Store.YES, Field.Index.TOKENIZED));
doc2.add(new Field("content", "诺基亚", Field.Store.YES, Field.Index.TOKENIZED));
doc2.add(new Field("content", "NOKIA", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.addDocument(doc2);
}
writer.optimize();
writer.close();
}
private void search() throws CorruptIndexException, IOException, ParseException{
IndexSearcher searcher = new IndexSearcher(PATH);
QueryParser parser = new QueryParser("content",getAnalyzer());
Query query = parser.parse(CONTENT);
Hits hits = searcher.search(query);
System.out.println(hits.length());
searcher.close();
}
private Analyzer getAnalyzer(){
return new StandardAnalyzer();
}
}