上一篇中我們使用多線程建立了索引,下面我們來試着采用不把多個索引目錄裡的資料合并到一個新的索引目錄的方式去查詢索引資料,當然你也可以合并(合并到一個索引目錄查詢就很簡單了),其實很多情況我們都是不合并到一個索引目錄的,那多索引目錄該如何查詢呢,在lucene5中使用的multireader類,在lucene4時代,使用的是multisearcher類。至于lucene多線程查詢,隻需要在建構indexsearcher對象時傳入一個executorservice線程池管理對象即可,具體請看下面貼出的示例代碼:
package com.yida.framework.lucene5.index;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
import java.util.concurrent.callable;
import java.util.concurrent.executionexception;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import java.util.concurrent.future;
import org.apache.lucene.document.document;
import org.apache.lucene.index.directoryreader;
import org.apache.lucene.index.indexreader;
import org.apache.lucene.index.multireader;
import org.apache.lucene.index.term;
import org.apache.lucene.search.indexsearcher;
import org.apache.lucene.search.query;
import org.apache.lucene.search.termquery;
import org.apache.lucene.store.directory;
import com.yida.framework.lucene5.util.luceneutils;
/**
* 多線程多索引目錄查詢測試
* @author lanxiaowei
*
*/
public class multithreadsearchtest {
public static void main(string[] args) throws interruptedexception, executionexception, ioexception {
//每個線程都從5個索引目錄中查詢,是以最終5個線程的查詢結果都一樣
//multithreadandmultireadersearch();
//多索引目錄查詢(把多個索引目錄當作一個索引目錄)
multireadersearch();
}
/**
* 多索引目錄查詢
* @throws interruptedexception
* @throws executionexception
* @throws ioexception
*/
public static void multireadersearch() throws interruptedexception, executionexception, ioexception {
directory directory1 = luceneutils.openfsdirectory("c:/lucenedir1");
directory directory2 = luceneutils.openfsdirectory("c:/lucenedir2");
directory directory3 = luceneutils.openfsdirectory("c:/lucenedir3");
directory directory4 = luceneutils.openfsdirectory("c:/lucenedir4");
directory directory5 = luceneutils.openfsdirectory("c:/lucenedir5");
indexreader reader1 = directoryreader.open(directory1);
indexreader reader2 = directoryreader.open(directory2);
indexreader reader3 = directoryreader.open(directory3);
indexreader reader4 = directoryreader.open(directory4);
indexreader reader5 = directoryreader.open(directory5);
multireader multireader = new multireader(reader1,reader2,reader3,reader4,reader5);
indexsearcher indexsearcher = luceneutils.getindexsearcher(multireader);
query query = new termquery(new term("contents","volatile"));
list<document> list = luceneutils.query(indexsearcher, query);
if(null == list || list.size() <= 0) {
system.out.println("no results.");
return;
}
for(document doc : list) {
string path = doc.get("path");
//string content = doc.get("contents");
system.out.println("path:" + path);
//system.out.println("contents:" + content);
* 多索引目錄且多線程查詢,異步收集查詢結果
public static void multithreadandmultireadersearch() throws interruptedexception, executionexception, ioexception {
int count = 5;
executorservice pool = executors.newfixedthreadpool(count);
final indexsearcher indexsearcher = luceneutils.getindexsearcher(multireader, pool);
final query query = new termquery(new term("contents","volatile"));
list<future<list<document>>> futures = new arraylist<future<list<document>>>(count);
for (int i = 0; i < count; i++) {
futures.add(pool.submit(new callable<list<document>>() {
public list<document> call() throws exception {
return luceneutils.query(indexsearcher, query);
}
}));
int t = 0;
//通過future異步擷取線程執行後傳回的結果
for (future<list<document>> future : futures) {
list<document> list = future.get();
if(null == list || list.size() <= 0) {
t++;
continue;
}
for(document doc : list) {
string path = doc.get("path");
//string content = doc.get("contents");
system.out.println("path:" + path);
//system.out.println("contents:" + content);
system.out.println("");
//釋放線程池資源
pool.shutdown();
if(t == count) {
}
當然你也可以把上面的代碼改造成每個線程查詢一個索引目錄,我上面是每個線程都從5個索引目錄中查詢,是以結果會列印5次,看到運作結果請不要感到奇怪。
如果你還有什麼問題請加我Q-q:7-3-6-0-3-1-3-0-5,
或者加裙
一起交流學習!
轉載:http://iamyida.iteye.com/blog/2196932