hibernate-search for one-directional associations

。_饼干妹妹 提交于 2019-12-11 05:36:00

问题


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.

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?


回答1:


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

Yes. I will fix this typo.

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

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. 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.

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.

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?

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

But:

  • you would need above-average knowledge of Lucene.
  • you would have to bypass Hibernate Search APIs, and would need to write code to do what Hibernate Search usually does.
  • you would have to use experimental (read: unstable) Lucene APIs.
  • I am unsure as to how well that would perform, as I never tried it.

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


来源:https://stackoverflow.com/questions/53719735/hibernate-search-for-one-directional-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!