With the purgeAllOnStart(false) its working
Be careful. This only works because Elasticsearch, by default, will create indexes and their mapping dynamically when it receives indexing requests for indexes that don’t exist yet. It will try to guess the type of fields, and very often, it will guess at least something wrong.
You really should not rely on that feature, and should instead create each Elasticsearch index in advance.
However, it seems that, for some reason, you don’t want to use the create
lifecycle strategy. This means you will have to create indexes manually.
Unfortunately, there currently isn’t any way to create the indexes manually through Hibernate Search.
Currently, your options are:
- Use the
create
or update
lifecycle strategy in your Elasticsearch backend, so that indexes are created on startup.
- Or bypass Hibernate Search and manually send HTTP requests to Elasticsearch to create the indexes.
The second solution is obviously much harder, because you have to know what the mappings will be.
Unless you have very deep knowledge of what your Elasticsearch mapping will look like, I would recommend starting Hibernate Search with the create
lifecycle strategy in a development environment, then getting the mappings from the Elasticsearch clusters and storing them in JSON files.
Then, in your application, you can send HTTP requests to the Elasticsearch cluster to create indexes before you do anything else, using the create index REST API.
To send requests to Elasticsearch, for now the best solution would be to create your own client. Hibernate Search does have APIs to access its internal REST client, but they are not currently accessible in the Hibernate ORM integration. I opened HSEARCH-3640 to fix that. In the meantime, I’d recommend the official, low-level REST client a community client such as Jest. There is also an official, high-level REST client but I’ve never used it.
In the next releases, we could consider exposing APIs so that you can manually trigger the creation of mappings through Hibernate Search. However, we’ll need more information on your use case:
- Why is the
create
lifecycle strategy not appropriate in your case?
- When do you want to create the indexes/mappings? On startup? On another event? Which one?
- Do you also need to delete the indexes? When?