【问题标题】:In a Lucene / Lucene.net search, how do I count the number of hits per document?在 Lucene / Lucene.net 搜索中,我如何计算每个文档的点击次数?
【发布时间】:2010-02-12 02:46:25
【问题描述】:

在搜索一堆文档时,我可以轻松找到符合我搜索条件的文档数量:

Hits hits = Searcher.Search(query);
int DocumentCount = hits.Length();

如何确定文档中的总命中数?例如,假设我搜索“congress”并返回 2 个文档。如何获得每个文档中出现“大会”的次数?例如,假设“会议”在文档 #1 中出现 2 次,在文档 #2 中出现 3 次。我正在寻找的结果是 5

【问题讨论】:

    标签: lucene lucene.net


    【解决方案1】:

    这是 Lucene Java,但应该适用于 Lucene.NET:

    List docIds = // doc ids for documents that matched the query, 
                  // sorted in ascending order 
    
    int totalFreq = 0;
    TermDocs termDocs = reader.termDocs();
    termDocs.seek(new Term("my_field", "congress"));
    for (int id : docIds) {
        termDocs.skipTo(id);
        totalFreq += termDocs.freq();
    }
    

    【讨论】:

    • @bajafresh4life:如果这个短语是“apple tree”之类的两个词呢?
    • 您想要该短语在每个文档或每个单词中出现的次数吗?
    【解决方案2】:

    这也是 Lucene Java。如果您的查询/搜索条件可以写成SpanQuery,那么您可以这样做:

    IndexReader indexReader = // define your index reader here
    SpanQuery spanQuery = // define your span query here
    Spans spans = spanQuery.getSpans(indexReader);
    int occurrenceCount = 0;
    while (spans.next()) {
        occurrenceCount++;
    }
    // now occurrenceCount contains the total number of occurrences of the word/phrase/etc across all documents in the index
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多