2017-02-13 79 views
0

我有一个解析查询来找到lucene.net与文件操作一起的单词,并在顺序操作查询解析器工作正常,当我使用Parallel.Foreach循环执行相同的操作我得到解析器异常。 下面的两个代码示例。Lucene.Net异常,同时在Parallel.ForEach循环解析查询<EOF>

工作代码:

#region - Non Parallel Execution - 

       using (var input = File.OpenText(file.FullName)) 
       { 
        using (var swFile = new StreamWriter(decryptingFilename)) 
        { 
         while ((line = input.ReadLine()) != null) 
         { 
          totalEncryptedContact++; 
          query = queryParser.Parse(line.Trim()); 
          TopDocs resultDocs = searcher.Search(query, 1); 
          if (resultDocs.ScoreDocs.Count() > 0) 
          { 
           hits = resultDocs.ScoreDocs[0]; 
           var documentFromSearcher = searcher.Doc(hits.Doc); 
           string msisdn = documentFromSearcher.Get("MSISDN"); 
           if (!string.IsNullOrWhiteSpace(msisdn)) 
           { 
            swFile.WriteLine(msisdn); 
            totalDecryptedContact++; 
           } 
          } 
         } 
        } 
       } 

       #endregion - Non Parallel Execution - 

异常代码与并行:

 using (var swFile = new StreamWriter(decryptingFilename)) 
     { 
      ParallelOptions options = new ParallelOptions(); 
      options.MaxDegreeOfParallelism = 4; 

      Parallel.ForEach(File.ReadLines(file.FullName), options, newline => 
      {      
       totalEncryptedContact++; 
       query = queryParser.Parse(QueryParser.Escape(newline.Trim()));      
       TopDocs resultDocs = searcher.Search(query, 1); 
       if (resultDocs.ScoreDocs.Count() > 0) 
       { 
        hits = resultDocs.ScoreDocs[0]; 
        var documentFromSearcher = searcher.Doc(hits.Doc); 
        string msisdn = documentFromSearcher.Get("MSISDN"); 
        if (!string.IsNullOrWhiteSpace(msisdn)) 
        { 
         swFile.WriteLine(msisdn); 
         totalDecryptedContact++; 
        } 
       } 
      }); 
     }   

例外: 类型的异常 'Lucene.Net.QueryParsers.ParseException' 发生在Lucene.Net.dll但在用户代码没有处理

其他信息:无法解析“7465”:遇到“”在第1行,第4栏

期待之一:

"(" ... 

"*" ... 

"^" ... 

<QUOTED> ... 

<TERM> ... 

<FUZZY_SLOP> ... 

<PREFIXTERM> ... 

<WILDTERM> ... 

"[" ... 

"{" ... 

<NUMBER> ... 

":" ... 

":" ... 

":" ... 

":" ... 

":" ... 

":" ... 

":" ... 

":" ... 

<TERM> ... 

"*" ... 

回答

1

QueryParser线程安全的。你可以在每次迭代中构造一个新的,但它们非常轻量。

此外,我怀疑你使用QueryParser.Escape会做你想要的。这将会转义所有QueryParser语法。所以如果你通过它,比如说fieldToSearch:"my text",它将会跳过引号和冒号等,并且在分析之后,你可能会得到一个如下所示的查询:defaultField:fieldtosearch defaultField:my defaultField:text