简体   繁体   English

如何使用Apache Lucene在顶部获得准确的搜索结果?

[英]How to get exact search results on top using Apache Lucene?

How to get best score searches on top using Apache Lucene? 如何使用Apache Lucene获得最佳成绩搜索?

1. State Authority
2. Authority State

Now user searches for "Authority State" or "State Authority", we are getting same results for both in above fashion. 现在用户搜索“ Authority State”或“ State Authority”,以上两种方式我们都得到相同的结果。 But for "Authority State" Search results should be 但对于“授权状态”,搜索结果应为

1. Authority State
2. State Authority

Following is lucene query on fields: 以下是对字段的lucene查询:

name:Authority State* 
name:Authority State
name:Authority*
name:State*

for (String field : INDEXED_FIELDS) {
           bool.should(qb.keyword().wildcard().onField(field).matching(userInputBuilder.toString()).createQuery());
        }

        for (String field : INDEXED_FIELDS) {
          for (String match : pattern) {
               bool.should(qb.keyword().onField(field).matching(match).createQuery());
          }
        }

There is no sorting on results. 没有对结果的排序。

Could anyone suggest how to get exact results? 有人可以建议如何获得准确的结果吗?

The keyword query type just looks to match the same tokens of the input, not taking into consideration the order. 关键字查询类型只是看起来与输入的相同标记匹配,而没有考虑顺序。

When you need it to take into consideration the order of the tokens within the phrase use the phrase query: 当您需要考虑词组中标记的顺序时,请使用词组查询:

Query query = queryBuilder
                .phrase()
                    .withSlop( 2 )//or other options of the Phrase query
                    .onField( field )
                    .sentence( userInputBuilder.toString() )
                .createQuery();

You might also be interested in trying out the latest "Simple Query Builder" . 您可能还对尝试使用最新的“简单查询生成器”感兴趣。

In case you're interested into "debugging" the scores, you could have the query engine output not just the results but also the score value and the evaluation formula used for each hit: 如果您有兴趣“调试”分数,则查询引擎不仅可以输出结果,还可以输出分数和用于每次匹配的评估公式:

List<Object[]> results = (List<Object[]>) fullTextSession
    .createFullTextQuery( mltQuery, Coffee.class )
    .setProjection( ProjectionConstants.THIS, ProjectionConstants.SCORE, ProjectionConstants.EXPLANATION )
    .list();

This will get you, for each hit, an array of three elements: 对于每次匹配,这将为您提供三个元素的数组:

  1. the matched entity instance 匹配的实体实例
  2. the score value 得分值
  3. a string explaining how it was scored 解释其得分方式的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM