简体   繁体   English

Lucene.Net并行搜索

[英]Lucene.Net Parallel Search

I am creating a Lucene.Net based search engine application in C# with a single index. 我正在用单个索引在C#中创建一个基于Lucene.Net的搜索引擎应用程序。 As a requirement, I need to optimize runtime for a test run with multiple (5) queries. 根据需要,我需要针对带有多个(5)查询的测试运行优化运行时。 Therefore, I wanted to use a separate thread for each search, returning results similar to this post. 因此,我想对每个搜索使用单独的线程,返回的结果与帖子类似。 My code looks like this: 我的代码如下所示:

// 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;

However, I am getting a Lucene.Net.QueryParser.ParseException see screenshot that I do NOT get when running the searches sequentially. 但是,我得到了Lucene.Net.QueryParser.ParseException, 请参阅按顺序运行搜索时没有得到的屏幕截图

Please comment if I let something unclear. 如果我不清楚,请发表评论。 I would appreciate any help on this issue. 在这个问题上的任何帮助,我将不胜感激。

You need to synchronize access to searchResults or multiple threads will modify it at the same time. 您需要同步对searchResults访问,否则多个线程将同时对其进行修改。 Or you can use the async pattern and return a Task<SearchResult> from your async method and not use the same List for every thread. 或者,您可以使用异步模式并从异步方法返回Task<SearchResult> ,而不是对每个线程使用相同的List

Also, declaring SearchResult result outside of the thread is just asking for trouble in the same way that searchResults is causing trouble. 同样,在线程外部声明SearchResult result只是在以与searchResults引起麻烦相同的方式来查找问题。 Declare it inside the thread. 在线程内部声明它。

Solved it. 解决了。 In the startSearch method I have a call to QueryParser which is not threadsafe as described here . startSearch方法中,我有一个QueryParser的调用,该调用不是此处所述的线程安全的。 Resolved this by adding a lock to the QueryParser instance. 通过向QueryParser实例添加lock来解决此问题。

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

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