2017-02-13 78 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