Java 多线程Lucene搜索
我需要在我的机器学习项目中以3GB大小的索引执行>3000个查询 为了提高性能,我创建了4个线程(我在macbook pro中有4个内核),并将总查询数的一部分分配给每个线程(如果我总共有N个查询,那么每个线程都有N/4个查询) 我通过Java 多线程Lucene搜索,java,multithreading,performance,lucene,Java,Multithreading,Performance,Lucene,我需要在我的机器学习项目中以3GB大小的索引执行>3000个查询 为了提高性能,我创建了4个线程(我在macbook pro中有4个内核),并将总查询数的一部分分配给每个线程(如果我总共有N个查询,那么每个线程都有N/4个查询) 我通过FSDirectory.open(file)打开索引,然后将indexsearch共享给所有线程 问题是,我看不到任何性能改进,cpu也没有增加。我玩了不同数量的线程,但仍然没有改变 在RAM中保存整个索引是不可能的 我在其他线程上看到一个解决方案是使用只读模式打
FSDirectory.open(file)
打开索引,然后将indexsearch
共享给所有线程
问题是,我看不到任何性能改进,cpu也没有增加。我玩了不同数量的线程,但仍然没有改变
在RAM中保存整个索引是不可能的
我在其他线程上看到一个解决方案是使用只读模式打开索引,但我使用lucene 4.3,其中写入选项从读卡器中删除,因此不再担心只读模式
我知道这些建议和建议,但看起来已经过时了
因此,我的问题是如何并行索引搜索以提高lucene的real性能
下面是我正在使用的示例代码:
List<String> queryList = new ArrayList<String>();
List<Thread> threads = new ArrayList<Thread>();
for(int i=0;i<NUMBER_THREADS;i++){
List<String> querySubList = queryList.subList(fromIndex, toIndex);
QueryParser ngramIndexQueryParser = new QueryParser(Version.LUCENE_43, "ngram", new KeywordAnalyzer());
startWorker(querySubList, threads, date, ngramIndexQueryParser, nGramSearcher);
}
public static void startWorker(List<String> querySubList, List<Thread> threads,QueryParser ngramIndexQueryParser,IndexSearcher nGramSearcher){
NGramIndexSearch task = new NGramIndexSearch(queryList, ngramIndexQueryParser, nGramSearcher);
Thread worker = new Thread(task);
worker.start();
threads.add(worker);
}
public class NGramIndexSearch implements Runnable {
public NGramIndexSearch(List<String> queryList, String year,QueryParser queryParser, IndexSearcher searcher){
//initialization
}
public void run() {
for(String q:queryList){
Query query = queryParser.parse(queryText);
TopDocs topDocs = searcher.search(query, nrOfDocsToReturn);
}
}
List queryList=new ArrayList();
List threads=new ArrayList();
对于(int i=0;我能给你看一些代码吗?创建线程时可能是qn错误。谢谢!我用示例代码更新了问题。你如何从索引和索引计算<代码>和<代码>到索引<代码>?我计算它们的正确匹配。已经测试过了。不要拘泥于功能性细节,我如何计算线程?你找到答案了吗?以单线程方式搜索3000个查询和750个查询之间的时间差是多少?