Hibbernate搜索无法在索引上使用一致

发布于 2025-01-24 11:55:09 字数 792 浏览 4 评论 0原文

我试图在@indexedembedded上进行汇总,不幸的是,在同一字段上的搜索效果不佳。

我的

Book {
...
@IndexedEmbedded
@ManyToOne
@JoinColumn(name="auth_id")
private Author auth;
...
}

Author {
...
@FullTextField
private String name;
...
}

Search {
...
AggregationKey<Map<String, Long>> countsByAuthKey = AggregationKey.of("countsByAuth");
SearchResult<Book> result = searchSession.search(Book.class).
                            where(f -> f.match().field("title").matching(title).
                            aggregation(countsByAuthKey, f -> f.terms().
                            field("auth.name", String.class)).fetchAll();

...

最后一行失败了,与“无法使用'contregation:enfor'air auth.name''on en auth.name'”。确保字段标记为可搜索/可分配/projectable/gropregable”(并且该字段是FullTextField)

I am trying to do an aggregation on @IndexedEmbedded, unfortunatly is not working well, but the search on the same field is working ok.

I have

Book {
...
@IndexedEmbedded
@ManyToOne
@JoinColumn(name="auth_id")
private Author auth;
...
}

Author {
...
@FullTextField
private String name;
...
}

Search {
...
AggregationKey<Map<String, Long>> countsByAuthKey = AggregationKey.of("countsByAuth");
SearchResult<Book> result = searchSession.search(Book.class).
                            where(f -> f.match().field("title").matching(title).
                            aggregation(countsByAuthKey, f -> f.terms().
                            field("auth.name", String.class)).fetchAll();

...

It fails on last line with "Cannot use 'aggregation:terms' on field 'auth.name'. Make sure the field is marked as searchable/sortable/projectable/aggregable" (And the field is fulltextfield)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2025-01-31 11:55:09

如错误消息中所述,您没有将字段标记为汇总。 参考文档

关于聚合有一些限制。特别是,为了使一个字段是“汇总”的,它必须为在映射中标记为,以便正确存储在索引中。

实际上,在这种情况下,您不能将字段标记为汇总:全文本字段不能成为聚合的目标。即使冬眠搜索允许,这些字段已被标记化,因此汇总会在作者的名字中返回一个单词(例如,“ John”和“ Smith”一个),这很可能不是不是你想要什么。

只需在全文本字段旁边创建另一个关键字字段即可。使用全文字段进行搜索,以及聚合的关键字字段。

Author {
...
@FullTextField
@KeywordField(name = "name_keyword", aggregable = Aggregable.YES)
private String name;
...
}
Search {
...
                            .aggregation(countsByAuthKey, f -> f.terms().
                            field("auth.name_keyword", String.class)).fetchAll();

...
}

在测试此内容之前,请记住,您需要在映射更改后重新创建索引并重新索引数据库。

As stated in the error message, you did not mark the field as aggregable. This is also mentioned in the reference documentation:

There are a few constraints regarding aggregations. In particular, in order for a field to be "aggregable", it must be marked as such in the mapping, so that it is correctly stored in the index.

And in fact, in this case you cannot mark the field as aggregable: Full-text fields cannot be the target of aggregations. Even if Hibernate Search allowed that, those fields are tokenized, so the aggregation would return one bucket per word in the author's name (e.g. one for "John" and one for "Smith"), which is most likely not what you want.

Just create another keyword field next to the full-text field. Use the full-text field for search, and the keyword field for aggregations.

Author {
...
@FullTextField
@KeywordField(name = "name_keyword", aggregable = Aggregable.YES)
private String name;
...
}
Search {
...
                            .aggregation(countsByAuthKey, f -> f.terms().
                            field("auth.name_keyword", String.class)).fetchAll();

...
}

Before you test this, remember that you need to re-create your indexes and reindex your database after a mapping change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文