【问题标题】:Hibernate search show match with keyword field on top then sort the rest休眠搜索显示与关键字字段匹配的顶部然后对其余部分进行排序
【发布时间】:2021-06-08 17:33:06
【问题描述】:

使用本地 lucene 的 Hibernate Search 6.0.1

我有一个搜索功能,用户可以在一个单词中搜索两个字段,其中一个是全文搜索,另一个是精确的关键字匹配。

Class A

@FullTextField(termVector = TermVector.YES, analyzer = "...", searchAnalyzer = "...")
private String text;

@KeywordField
private String keyword;

@IndexedEmbedded(includePaths = {"number"})
@OneToOne(mappedBy = "a", fetch = FetchType.LAZY)
private Stats stats;

我的搜索功能如下;

searchSession.search(A.class)
.where(f -> f.bool()
        .should(f.match().field("text").matching(query))
        .should(f.match().field("keyword").matching(query))
)
.sort(f -> f.field("stats.number").desc().missing().last())
.fetch(offset, limit);

我想搜索结果按数字排序的这两个字段。但是,我还想在搜索结果的顶部显示与关键字的完全匹配。 (关键字是唯一的)

为了实现这一点,我应该怎么做? 我是否需要将搜索功能分成两个不同的查询然后合并它们?还是有更好的做法?

【问题讨论】:

    标签: hibernate lucene hibernate-search nhibernate-search


    【解决方案1】:

    使用score 排序和每个预测提升将给定谓词上的匹配放在结果列表中的较高位置。然后使用字段排序对具有相同分数的结果进行排序。

    这应该可以解决问题:

    searchSession.search(A.class)
    .where(f -> f.bool()
            .should(f.match().field("text").matching(query)
                .constantScore().boost(1.0f))
            .should(f.match().field("keyword").matching(query)
                .constantScore().boost(2.0f))
    )
    .sort(f -> f.score().then().field("stats.number").desc().missing().last())
    .fetch(offset, limit);
    

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多