Hibbernate搜索无法在索引上使用一致
我试图在@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如错误消息中所述,您没有将字段标记为汇总。 参考文档:
实际上,在这种情况下,您不能将字段标记为汇总:全文本字段不能成为聚合的目标。即使冬眠搜索允许,这些字段已被标记化,因此汇总会在作者的名字中返回一个单词(例如,“ John”和“ Smith”一个),这很可能不是不是你想要什么。
只需在全文本字段旁边创建另一个关键字字段即可。使用全文字段进行搜索,以及聚合的关键字字段。
在测试此内容之前,请记住,您需要在映射更改后重新创建索引并重新索引数据库。
As stated in the error message, you did not mark the field as aggregable. This is also mentioned in the reference documentation:
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.
Before you test this, remember that you need to re-create your indexes and reindex your database after a mapping change.