我在Lucene_35的字段中搜索。我想知道我的术语中有多少单词与该领域相匹配。 例如,我的领域是“JavaServer Faces(JSF)是一个基于Java的Web应用程序框架,旨在简化基于Web的用户界面的开发集成。”,我的查询词是的 “java/JSF /框架/ doesnotexist”和希望导致3,因为只有的 “java”,“JSF”和“框架”出现在现场。 这里是我下面一个简单的例子:Lucene计数匹配项
public void explain(String document, String queryExpr) throws Exception {
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
IndexWriter w = new IndexWriter(index, config);
addDoc(w, document);
w.close();
String queryExpression = queryExpr;
Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(queryExpression);
System.out.println("Query: " + queryExpression);
IndexReader reader = IndexReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(q, 10);
for (int i = 0; i < topDocs.totalHits; i++) {
ScoreDoc match = topDocs.scoreDocs[i];
System.out.println("match.score: " + match.score);
Explanation explanation = searcher.explain(q, match.doc); //#1
System.out.println("----------");
Document doc = searcher.doc(match.doc);
System.out.println(doc.get("title"));
System.out.println(explanation.toString());
}
searcher.close();
}
与上述参数的输出是:
0.021505041 = (MATCH) product of:
0.028673388 = (MATCH) sum of:
0.0064956956 = (MATCH) weight(title:java in 0), product of:
0.2709602 = queryWeight(title:java), product of:
0.30685282 = idf(docFreq=1, maxDocs=1)
0.8830299 = queryNorm
....
0.033902764 = (MATCH) fieldWeight(title:framework in 0), product of:
1.4142135 = tf(termFreq(title:framework)=2)
0.30685282 = idf(docFreq=1, maxDocs=1)
0.078125 = fieldNorm(field=title, doc=0)
0.75 = coord(3/4)
我想这3/4作为结果。
问候!
它与Lucene有什么关系? – jpountz 2012-02-28 17:00:26
对不起jpountz,你是什么意思?我正在使用LUCENE_35和RAMDirectory索引。现在我意识到,有一个协调因素,它给了我确切的需要,但不知道如何得到这个协调因子。 – 2012-02-28 19:14:07
你的问题没有提到Lucene,所以我不确定你的问题与Lucene有什么关系。你可以通过更多的细节来修改你的问题吗?你的索引结构如何?你希望你的文件根据比赛数量进行排序吗? – jpountz 2012-02-28 19:26:17