休眠搜索单向关联 [英] hibernate-search for one-directional associations

查看:88
本文介绍了休眠搜索单向关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据规范,当 @IndexedEmbedded指向实体时,关联必须是定向的,另一端必须使用@ContainedIn进行注释.如果不是这样,当关联的实体更新时,Hibernate Search将无法更新根索引.

According to the spec, when @IndexedEmbedded points to an entity, the association has to be directional and the other side has to be annotated with @ContainedIn. If not, Hibernate Search has no way to update the root index when the associated entity is updated.

我对 direction 一词应该是 bidirectional_em 正确吗?我确实有一个问题,我的索引没有更新.我有单向关系,例如订购的人,但订购的人不认识.现在,当我更改顺序时,索引不会更新.

Am I right to assume the word directional should be bi-directional? I have exactly the problem that my index is not updated. I have one-directional relationships, e.g. person to order but the order does not know the person. Now when I change the order the index is not updated.

如果将关联更改为双向是没有选择的,那么我仍然必须使用休眠搜索吗?是否可以创建两个单独的索引并合并查询?

If changing the associations to become bi-directional is no option which possibilities would I have to still use hibernate-search? Would it be possible to create two separate indices and to combine queries?

推荐答案

我对方向这个词应该是双向的吗?

Am I right to assume the word directional should be bi-directional?

是的.我会解决这个错字.

Yes. I will fix this typo.

如果无法将关联更改为双向,那么我仍然必须使用hibernate-search吗?

If changing the associations to become bi-directional is no option which possibilities would I have to still use hibernate-search?

如果对Person进行了索引并嵌入了Order,但是OrderPerson没有反向关联,则当Person. >更改.

If Person is indexed and embeds Order, but Order doesn't have an inverse association to Person, then Hibernate Search cannot retrieve the Persons that have to be reindexed when an Order changes.

因此,您将必须手动重新编制索引:

Thus you will have to reindex manually: https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#manual-index-changes .

您可以采用以下两种策略之一:

You can adopt one of two strategies:

  1. 简单的方法:定期为所有Person实体重新编制索引,例如每天晚上.
  2. 硬路径:Order更改时,重新为受影响的Person实体编制索引.这基本上意味着向服务中添加代码,以便每当创建/更新/删除订单时,您都运行查询以检索所有相应人员,并手动为其重新编制索引.
  1. The easy path: reindex all the Person entities periodically, e.g. every night.
  2. The hard path: reindex the affected Person entities whenever an Order changes. This basically means adding code to your services so that whenever an order is created/updated/deleted, you run a query to retrieve all the corresponding persons, and reindex them manually.

第一个解决方案相当简单,但缺点是Person索引最多可能过24小时.取决于您的用例,这可能没事,也可能没事.

The first solution is fairly simple, but has the big disadvantage that the Person index will be up to 24 hours out of date. Depending on your use case, that may be ok or that may not.

第二种解决方案容易出错,基本上您将在进行Hibernate Search的工作.

The second solution is prone to errors and you would basically be doing Hibernate Search's work.

总而言之,您真的必须问自己,将关联的反面添加到模型中是否会更好?

All in all, you really have to ask yourself if adding the inverse side of the association to your model wouldn't be better.

是否可以创建两个单独的索引并合并查询?

Would it be possible to create two separate indices and to combine queries?

从技术上讲,如果您使用的是Lucene集成(而不是Elasticsearch集成),那么可以.

Technically, if you are using the Lucene integration (not the Elasticsearch one), then yes, it would be possible.

但是:

  • 您将需要高于平均水平的Lucene知识.
  • 您将不得不绕过Hibernate Search API,并且需要编写代码来执行Hibernate Search通常要做的事情.
  • 您将不得不使用实验性的(阅读:不稳定的)Lucene API.
  • 我不确定该如何执行,因为我从未尝试过.

因此,如果您不熟悉Lucene的API,我将不建议您这样做.如果您真的想走这条路,这里有一些提示:

So I wouldn't recommend it if you're not familiar with Lucene's APIs. If you really want to take that path, here are a few pointers:

  • How to use the index readers directly: https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#IndexReaders
  • Lucene's documentation for joins (what you're looking for is query-time joins): https://lucene.apache.org/core/5_5_5/join/org/apache/lucene/search/join/package-summary.html

这篇关于休眠搜索单向关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆