首先罗列会使用的特殊类(常用的不再赘述):
TrackingIndexWriter 追踪writer,在api中有介绍,只有通过这个类进行更新ControlledRealTimeReopenThread才能获得更新
ControlledRealTimeReopenThread 实时搜索其实叫做“近实时搜索”,就是当更新之后在很短时间内进行了更新然后让人感觉不出来,这样说来就需要一个守护线程去坚守这个过程,当更新的时候就去获取更新然后通知查询更新了,这样“近实时搜索”就实现了,这个类就是这个守护线程。
我写了个测试在下面,里面不懂得可以查看api,也有部分注释,希望对大家有帮助。
package com.xikoubuy.main;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.TrackingIndexWriter;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.ControlledRealTimeReopenThread;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ReferenceManager;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class NRTTest {
private Directory directory;
private Analyzer analyzer;
////追踪writer,这样才能在更新之后通知搜索
private TrackingIndexWriter writer;
//是线程安全的.第二个参数是是否在所有缓存清空后让search看到
private SearcherManager searcherManager;
private IndexSearcher searcher;
public static void main(String[] args) throws IOException {
new NRTTest();
}
public NRTTest() throws IOException {
directory = new RAMDirectory();
analyzer = new IKAnalyzer(true);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
writer = new TrackingIndexWriter(indexWriter);
searcherManager = new SearcherManager(indexWriter, true, new SearcherFactory());
ControlledRealTimeReopenThread CRTReopenThead = new ControlledRealTimeReopenThread(writer, searcherManager, 5.0, 0.025);
//守护线程,又叫后台线程,级别比较低,如果没有主线程这个也会消失,这个线程作用就是定期更新让searchManager管理的search能获得更新
CRTReopenThead.setDaemon(true);
CRTReopenThead.setName("更新线程");
CRTReopenThead.start();
this.addDoc();
this.searchDoc();
}
public synchronized void addDoc(){
final int i = 0;
new Thread(){
public void run() {
while(true){
try {
Thread.sleep(10000);
System.out.println("----加入文档中");
Document doc = new Document();
doc.add(new Field("title", "标题" + i, TextField.TYPE_STORED));
doc.add(new Field("content", "我爱你中国" + i, TextField.TYPE_STORED));
writer.addDocument(doc);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
public synchronized void searchDoc(){
new Thread(){
public void run() {
while(true){
try {
Thread.sleep(5000);
System.out.println("----检索中");
searcher = searcherManager.acquire();
QueryParser parser = new QueryParser("content", analyzer);
Query query = parser.parse("中国");
ScoreDoc [] hits = searcher.search(query, 100).scoreDocs;
for(int i = 0; i < hits.length;i++){
Document doc = searcher.doc(hits[i].doc );
System.out.println(doc.get("title") + hits[i].doc);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
searcherManager.release(searcher);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
searcher = null;
}
};
}.start();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/whybangbang/article/details/47011991