hibernate-searchhibernate-search-6

Hibernate search 6 : updating EmbededIndex from another Microservice


Good morning, I have a rather special scenario and I would like to have your opinion on the best way to handle this situation. We have an application divided into several functional microservices, but a common database (it's not ideal but for the moment we have no choice). From a microservice A, I index entity A with entities B, C and D, like IndexedEmbeded. 1- if I make modifications on A, by changing B or C or D, is it automatically propagated in the indexing document or does it require additional configuration? 2- the tables of entities B, C and D are updated by other microservices and in this case I have to update my index of entity A. What is the best way to do this?

I thought of doing manual indexing trimmed every change in the other microservices. but I'm not sure that's the best way to do it.

Thank you


Solution

  • I'll state the obvious and say that if you use the same model across microservices, you're in for some headaches, especially when updating your schema, but I guess you know that and can't do anything about it. So, let's have a look at solutions...

    if I make modifications on A, by changing B or C or D, is it automatically propagated in the indexing document or does it require additional configuration

    Assuming everything happens in the same microservice, and the updates are performed using Hibernate ORM (and not native SQL), it should be automatic. See https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-reindexing-basics .

    the tables of entities B, C and D are updated by other microservices and in this case I have to update my index of entity A. What is the best way to do this?

    Assuming your other microservices share the same Hibernate ORM mapping (they know of entity A, they just don't deal with it), e.g. they all import a common JAR that contains your annotated entities... you could simply rely on outbox-polling coordination, which allows multiple instances of an application (or of different applications with the same model/mapping) to cooperate and index safely and reliably, as long as they all use Hibernate Search with compatible configuration.

    If that's not your case, e.g. each microservice has its own entity classes, and may not include all entities from other microservices... I'm afraid Hibernate Search can't solve that problem for you (yet). Hibernate Search exposes a way to trigger reindexing based on entity events (entity created, entity property 'foo.bar' updated, entity deleted, ...) that you input manually, via the SearchIndexingPlan, but you will have to devise a way to propagate these events from one microservice to another. And that kind of makes Hibernate Search a lot less useful, unfortunately.