首页 > 解决方案 > Hibernate Search Mass Indexer 不会删除映射

问题描述

我目前正在使用 MassIndexer 来重新索引我的实体:

fullTextSession.createIndexer().startAndWait();

但是,我了解到 MassIndexer 不会删除现有的映射。似乎删除映射的唯一方法是将其设置index_schema_management_strategy为“drop-and-create”,不建议在生产环境中使用。

在使用 MassIndexer 重新索引之前,我曾尝试使用 DELETE 索引 API 直接点击弹性搜索,但这会在我们的映射中引入奇怪的行为。

删除索引及其映射,然后使用 MassIndexer 重建该索引的推荐方法是什么?

标签: elasticsearchhibernate-search

解决方案


我找到了一种让休眠搜索完全删除映射和索引的方法,尽管这是一个相当好的解决方法。

通过实例化ElasticSearchService这样的:

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

然后,您可以访问以下类:

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

并执行以下操作:

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

这基本上就是drop-and-create配置设置将为您做的事情。我们计划将其设置为一些外部服务,以便在我们想要完全重建索​​引时使用——包括映射和文档。

不幸的是,这感觉非常hacky,并且奇怪的是似乎没有更好的方法来做到这一点。


推荐阅读