休眠搜索每次命中检查谓词

问题描述 投票:0回答:1

我在 hibernate search 6.2 中使用 lucene 后端做了类似的事情:

final var searchSession = Search.session(em);
final var orgas = searchSession
  .search(Organization.class)
  .where(...)
  .hits();

现在我想知道每次命中谓词

p
是否为真。我考虑了以下方法。

  1. 获取谓词的结果作为上面查询的一部分。这很好,但我不知道如何?这可能吗?

  2. 再次搜索

    Organization.class
    ,其中
    where
    必须与先前命中的 id 以及谓词相匹配。然后我可以查找某个组织是否也是该结果的一部分,从而知道该谓词是否为真。但是通过 id 匹配并运行两个查询感觉很奇怪。

  3. 再次执行完全相同的搜索,但使用添加的谓词。与之前的方法相比,该方法可以摆脱 id 匹配,但需要执行原始(更复杂的)查询两次,并且会弄乱分页。

有什么好的解决方案吗?

hibernate-search
1个回答
0
投票

您还可以查看使用分数和布尔谓词。您可以将要“检查”的谓词作为 should 子句,并使用过滤器将结果限制为当前在 where 子句中使用的任何内容。看起来像这样:

.search(Organization.class)
.select( f -> f.composite().from(
        f.score(),
        f.id(),
        ...
).asList() )
.where( f -> f.bool()
        // this predicate will be used for scoring the hits.
        // if the score == 0 that would mean the predicate didn't match
        .should( your predicate p )
        // you will only get the same results as you are getting now: 
        .filter( your current where predicate )
)

bool 运算符中的过滤器将执行过滤,您只能得到与该过滤器中的谓词匹配的结果。并且应该谓词将用于对命中进行评分。这是指向 bool 谓词文档的链接,以获取任何其他信息。

© www.soinside.com 2019 - 2024. All rights reserved.