2010-01-13 121 views
0

使用Lucene,我可以計算出如何創建文檔,將值放在受尊重的字段中,然後繼續使用搜索器在索引文檔中搜索匹配項。在文檔字段中計算正則表達式查詢匹配的數量

但是,我現在更關心每個文檔的特定字段中匹配的數量。只知道有一場比賽沒有問題,但我想知道場上有多少次這種模式。

例子。

Document doc = new Document(); 
doc.add(new Field("TNAME", "table_one", Field.Store.YES, Field.Index.NOT_ANALYZED)); 
doc.add(new Field("CNAME", "column_one", Field.Store.YES, Field.Index.NOT_ANALYZED)); 
doc.add(new Field("DATA", "This would be the data found in this particular field of a single document", Field.Store.NO, Field.Index.ANALYZED)); 

如果我想大跳文檔搜索查詢「DATA」欄找出次^ d的數量。*模式滿足我會怎麼做呢? (給上述文件2的結果)。

+0

好吧,我發現了一個方法來計算匹配特定的正則表達式的一些術語: 的IndexReader讀卡器= IndexReader.open(目錄); RegexTermEnum = new RegexTermEnum(reader,new Term(「field」,「^ d。*」),new JavaUtilRegexCapabilities()); 但是,我仍然對如何搜索完整索引以及在每個文檔的字段中找到正則表達式模式匹配的頻率感到迷茫。 我猜我需要的兩條信息是: 1)索引中的哪個文檔有一個或多個查詢匹配。 2)在每個文檔/字段中找到正則表達式查詢的次數。 – user250117 2010-01-14 18:15:03

回答

0

簡單的答案發現:

IndexSearcher searcher = new IndexSearcher(directory); 
    IndexReader reader = searcher.getIndexReader(); 
    RegexTermEnum regexTermEnum = new RegexTermEnum(reader, new Term(
      "field", "d.*"), new JavaUtilRegexCapabilities()); 

    do { 
     System.out.println("Next:"); 
     System.out.println("\tDoc Freq: " + regexTermEnum.docFreq()); 
     if (regexTermEnum.term() != null) {    
      System.out.println("\t"+regexTermEnum.term()); 
      TermDocs td = reader.termDocs(regexTermEnum.term()); 
      while(td.next()){ 
       System.out.println("Found "+ td.freq()+" matches in document " + reader.document(td.doc()).get("name")); 
      } 
     } 
    } while (regexTermEnum.next()); 
    System.out.println("End."); 
+0

Lates Lucene版本(6.6)中RegexTermEnum的等效類是什麼? – getSantsoh 2017-09-13 15:26:44