Hibernate Search 6 + Elastic Search 7+ Integration Test

So everything seems working in live application however when we wrote Integration test we have all the test cases failing without any exceptions.
It seems like since we are using remote Elastic server for indexing and in test cases when we modify the entities and the reflected indexes are before updating test will ask for data and thus it is failing.
Can we have any workaround for this as we are working in agile team and integration tests are must.

Elasticsearch is a near-real-time platform. When you change something in the index, Elasticsearch will return a response and tell you everything was indexed, but the changes will not be visible when searching immediately. By default it will take about one second for the changes to be visible in a search query.

This is the behavior of Elasticsearch, not Hibernate Search, and it behaves that way for performance reasons.

In integration tests this behavior is inconvenient since you want to check the changes had an effect right away.
Thus, for tests, (not in production!), you might want to tell Hibernate Search to make changes visible immediately after they are performed. This can be achieved by setting this property:

hibernate.search.automatic_indexing.synchronization.strategy = sync

Again, you should not do this in production environments: indexing throughput will decrease significantly.

I encourage you to read the documentation for more information about this behavior, in particular this section.

1 Like

@yrodiere thanks for suggestion however I tried it and tests were still failing!
though I have done one trick to update index for test cases
In before method explicitly put to reindex data and that way its working .
I know this is not good approach however I haven’t find anything except this to overcome this issue.
Please suggest if u have anything to share on this

How do you initialize your database for tests?

If you use SQL commands, or JPQL, or if you restore a database snapshot, then Hibernate Search is not aware of the data you put into the database. In that case, then yes, mass indexing is the way to go.