代码之家  ›  专栏  ›  技术社区  ›  Keltex

在lucene/lucene.net搜索中,如何计算每个文档的点击数?

  •  6
  • Keltex  · 技术社区  · 15 年前

    在搜索一组文档时,我可以很容易地找到符合搜索条件的文档数:

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

    如何确定文档中的点击总数?例如,假设我搜索“国会”,我得到了2份文件。我如何才能得到“国会”在每个文件中出现的次数?例如,假设“国会”在文件1中出现2次,在文件2中出现3次。我要找的结果是 .

    2 回复  |  直到 11 年前
        1
  •  6
  •   M.Babcock    11 年前

    这是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();
    }
    
        2
  •  0
  •   Kai Chan    15 年前

    这也是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