繁体   English   中英

Lucene.Net并行搜索

[英]Lucene.Net Parallel Search

我正在用单个索引在C#中创建一个基于Lucene.Net的搜索引擎应用程序。 根据需要,我需要针对带有多个(5)查询的测试运行优化运行时。 因此,我想对每个搜索使用单独的线程,返回的结果与帖子类似。 我的代码如下所示:

// load information needs
List<InformationNeed> informationNeeds = FileReader.readInformationNeeds(collectionPath);             

// each search has its own thread referenced in this list
List<Thread> threadList = new List<Thread>();

// each search has its own result referenced in this list
List<SearchResult> searchResults = new List<SearchResult>();


foreach (InformationNeed informationNeed in informationNeeds){

    // create searchOptions
    SearchOptions searchOptions = new SearchOptions(DEBUG_pre_processQuery, informationNeed.getInput());

    // run search
    SearchResult result = null; // Used to store the return value
    var thread = new Thread(
       () =>
       {
       result = startSearch(searchOptions);
       Console.WriteLine("end search for IN nr " + informationNeed.getID() + "results = " + result);

       //add results to list
       searchResults.Add(result);

       });
    thread.Start();
    threadList.Add(thread);
}

// block main thread until all threads finished
foreach (Thread t in threadList){
    t.Join();
}

return searchResults;

但是,我得到了Lucene.Net.QueryParser.ParseException, 请参阅按顺序运行搜索时没有得到的屏幕截图

如果我不清楚,请发表评论。 在这个问题上的任何帮助,我将不胜感激。

您需要同步对searchResults访问,否则多个线程将同时对其进行修改。 或者,您可以使用异步模式并从异步方法返回Task<SearchResult> ,而不是对每个线程使用相同的List

同样,在线程外部声明SearchResult result只是在以与searchResults引起麻烦相同的方式来查找问题。 在线程内部声明它。

解决了。 startSearch方法中,我有一个QueryParser的调用,该调用不是此处所述的线程安全的。 通过向QueryParser实例添加lock来解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM