lucene 在查询中获得匹配的术语

2023-06-29Java开发问题
4

本文介绍了lucene 在查询中获得匹配的术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 lucene 中找出与给定文档匹配的查询中的哪些词的最佳方法是什么?

What is the best way to find out which terms in a query matched against a given document returned as a hit in lucene?

我尝试了一种奇怪的方法,涉及 lucene contrib 中的命中突出显示包,以及一种针对最顶层文档搜索查询中每个单词的方法(docId: xy AND description: each_word_in_query").

I have tried a weird method involving hit highlighting package in lucene contrib and also a method that searches for every word in the query against the top most document ("docId: xy AND description: each_word_in_query").

没有得到满意的结果?命中突出显示不报告与第一个文档以外的文档匹配的某些单词.我不确定第二种方法是否是最佳选择.

Do not get satisfactory results? Hit highlighting does not report some of the words that matched for a document other than the first one. I'm not sure if the second approach is the best alternative.

推荐答案

方法explain 在 Searcher 中是查看查询的哪个部分匹配以及如何匹配的好方法它会影响总分.

The method explain in the Searcher is a nice way to see which part of a query was matched and how it affects the overall score.

示例取自《Lucene In Action 2nd Edition》一书:

Example taken from the book Lucene In Action 2nd Edition:

public class Explainer {

  public static void main(String[] args) throws Exception {

     if (args.length != 2) {
        System.err.println("Usage: Explainer <index dir> <query>");
        System.exit(1);
     }

     String indexDir = args[0];
     String queryExpression = args[1];
     Directory directory = FSDirectory.open(new File(indexDir));
     QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,
                                     "contents", new SimpleAnalyzer());

     Query query = parser.parse(queryExpression);
     System.out.println("Query: " + queryExpression);
     IndexSearcher searcher = new IndexSearcher(directory);
     TopDocs topDocs = searcher.search(query, 10);
     for (int i = 0; i < topDocs.totalHits; i++) {
        ScoreDoc match = topDocs.scoreDocs[i];
        Explanation explanation = searcher.explain(query, match.doc);   
        System.out.println("----------");
        Document doc = searcher.doc(match.doc);
        System.out.println(doc.get("title"));
        System.out.println(explanation.toString());
     }
  }
}

这将解释与查询匹配的每个文档的分数.

This will explain the score of each document that matches the query.

这篇关于lucene 在查询中获得匹配的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13