2

I am currently using the MassIndexer like so to reindex my entities:

fullTextSession.createIndexer().startAndWait();

However, I have learned that the MassIndexer does not drop the existing mappings. It seems like the only way to drop mappings is to set the index_schema_management_strategy to 'drop-and-create', which is not recommended to be used in a production environment.

I have tried hitting elastic search directly using the DELETE index API before reindexing with the MassIndexer, but that introduces strange behavior with our mappings.

What is the recommended way to drop an index and it's mappings, and then rebuild that index using the MassIndexer?

logeyg
  • 549
  • 2
  • 8
  • 31

2 Answers2

1

I've found a way to get hibernate search to completely drop mappings and indexes, although this has been quite a workaround.

By instantiating an ElasticSearchService like so:

SearchIntegrator si = org.hibernate.search.orm.spi.SearchIntegratorHelper.extractFromEntityManagerFactory( fullTextSession.getEntityManagerFactory() );
ElasticsearchService elasticsearchService = si.getServiceManager().requestService(ElasticsearchService.class);

You can then access the following classes:

ElasticsearchSchemaDropper schemaDropper = elasticsearchService.getSchemaDropper();
ElasticsearchSchemaCreator schemaCreator = elasticsearchService.getSchemaCreator();

And do the following:

schemaDropper.drop(URLEncodedString.fromString("index you want to drop"), options);
schemaCreator.createIndex( indexMetadata, options );
schemaCreator.createMappings( indexMetadata, options );

This is essentially what the drop-and-create configuration setting will do for you. We plan on setting this up as some external service to hit whenever we want to completely rebuild our index - both the mappings and the documents.

Unfortunately, this feels very hacky, and it is curious that there doesn't seem to be a better way to do this.

logeyg
  • 549
  • 2
  • 8
  • 31
0

The purpose of the MassIndexer is to help update the index content as a disaster recovery strategy or to build the initial index state, as normally the index is kept in synch.

It is not meant ro perform more advanced lifecycle operations on the index such as live schema changes.

For that I would suggest that you should at least stop/restart the Hibernate application (so you can use that other property) and most likely invoke index management operations via external scripts or as part of your release process.

Letting Hibernate Search manage the index schema is meant as a convenience during development.

Sanne
  • 6,027
  • 19
  • 34
  • In order to leverage all the annotations that hibernate search provides, such as analyzers, normalizers, etc, isn't it necessary to use hibernate search to manage the index schema, whether in a development or production environment? – logeyg Aug 17 '18 at 17:11
  • @logeyg You can just let Hibernate Search create the schema in development environment, dump it, check it, then apply it on your production environment. The important bit being "check it", here. But it will definitely be easier to do once we implement [HSEARCH-2366](https://hibernate.atlassian.net/browse/HSEARCH-2366) – yrodiere Aug 27 '18 at 08:42