gpt4 book ai didi

java - lucene 的总点击次数

转载 作者:行者123 更新时间:2023-12-01 15:41:27 25 4
gpt4 key购买 nike

我正在 Lucene 中运行一个程序。我得到每个单词的点击总数。这意味着它会获取包含我搜索的单词的所有文件。

示例:

Searching for 'Amazon'
Number of hits: 2
Hit: Files\peru.txt
Hit: Files\correspondent.txt
Searching for 'business'
Number of hits: 5
Hit: Files\innovation.txt
Hit: Files\xmas.txt
Hit: Files\bp.txt
Hit: Files\symbian.txt
Hit: Files\peru.txt
Searching for 'environment'
Number of hits: 3
Hit: Files\food.txt
Hit: Files\sarkozy.txt
Hit: Files\symbian.txt

我的第一个问题是如何添加整个查询的命中总数(2+5+3)并显示它们。

我的第二个问题是如何按顺序显示结果?从 2 然后 3 然后 5

如有任何建议,我们将不胜感激!!

搜索索引的代码和上述输出:

public static void searchIndex(String searchString) throws IOException, ParseException {
int counter = 0 ;



System.out.println("Searching for '" + searchString + "'");
Directory directory = FSDirectory.getDirectory(INDEX_DIRECTORY);
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);

Analyzer analyzer = new StandardAnalyzer();
QueryParser queryParser = new QueryParser(FIELD_CONTENTS, analyzer);
Query query = queryParser.parse(searchString);
Hits hits = indexSearcher.search(query);
System.out.println("Number of hits: " + hits.length());



Iterator<Hit> it = hits.iterator();
while (it.hasNext()) {
Hit hit = it.next();
Document document = hit.getDocument();
String path = document.get(path1);
System.out.println("Hit: " + path);
}

}
}

问候。

最佳答案

使用Searcher.search获取每个关键字的 TopDocs,然后按成员 TopDocs.totalHits 求和/排序.

如果您只需要统计信息,search 的第二个参数应该不重要。如果您想查找所有命中,请将其设置为索引中的文档数,因为这是命中数的一个微不足道的上限。

关于java - lucene 的总点击次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com