不显示删除回复显示所有回复显示星级回复显示得分回复 lucene如何统计一个关键词在一个文章中出现的次数
就是想知道,在一篇文章中一个关键字出现了多少次,用lucene应该怎么做呢?
比如一篇文章
dog pig dog pig dog dog bird
我用dog最关键词查找,就直接告诉我这篇文章中dog出现了4次就可以了。
我在网上看到一些代码,但是都不能用,也看不太明白。希望那位能帮帮忙。最好能给个代码。谢谢啦。
对问题的补充
比如下面这个已经写好的查询方法。我想在红色的部分添加一下统计该关键字在这个document上出现的次数
public void search() throws Exception {
//想查询的关键字
String queryString = "document";
IndexSearcher indexSearcher = null;
try {
//indexPath:索引所创建的位置,是硬盘里面的一个位置
indexSearcher = new IndexSearcher(indexPath);
//在文章的content属性中,用analyzer作为分词器,创建对应的查询对象,
QueryParser queryParser = new QueryParser("content", analyzer);
Query query = queryParser.parse(queryString);
Filter filter = null;
//只罗列出查询结果的前n个
int n = 1000;
//查询结果不止一个,这里TopDocs是一个将诶过的集合
TopDocs topDocs = indexSearcher.search(query, filter, n);
//共有多少条匹配结果topDocs.totalHits
System.out.println("共有 " + topDocs.totalHits + " 条匹配的结果");
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
//scoreDoc保存的是查询出来的Document的在结果集合中的一个编号。注意这里查询对应Document对象的方法
Document doc = indexSearcher.doc(scoreDoc.doc); // 通过内部编号取出相应的Document
在这里统计一下关键字在这个doc上出现的次数。
System.out.println("===================");
// doc.getField("title").stringValue();
System.out.println("title=" + doc.get("title"));
System.out.println("content=" + doc.get("content"));
}
} finally {
indexSearcher.close();
}
}