Skip to content

Commit

Permalink
HSEARCH-2531 Make configuration-based index name override work with E…
Browse files Browse the repository at this point in the history
…lasticsearchIndexManager

We used to apply this override on the attribute returned by
getIndexName(), which is really not a good idea: Hibernate Search works
in such a way that it expects the index name returned by getIndexName()
to be the exact same that was passed to the initialize() method.

Because we did change the returned getIndexName(), we couldn't retrieve
the index manager anymore when indexing and indexing failed
consistently.

Possible solutions were:

 * to align ElasticsearchIndexManager's behavior on that of
   DirectoryBasedIndexManager, making getIndexName() return the exact
   string that was passed to the initialize() method.
 * to make the index name passed to the initialize() method already
   account for the override. But that would be a breaking change to
   existing IndexManager implementations.
 * to make Hibernate Search ask newly created index managers for their
   actual name after initializing them (by calling getActualName is
   available or getIndexName otherwise). But that would be a breaking
   change to clients of SearchIntegrator.getIndexManager.

I chose the first solution, which is more hackish, but safer in the
current context of a candidate release.
  • Loading branch information
yrodiere authored and Sanne committed Jan 22, 2017
1 parent 49daf5d commit 1b76b41
Showing 1 changed file with 21 additions and 3 deletions.
Expand Up @@ -59,8 +59,25 @@ public class ElasticsearchIndexManager implements IndexManager, IndexNameNormali

static final Log LOG = LoggerFactory.make( Log.class );

/**
* The index name for Hibernate Search, which is actually
* the index <em>manager</em> name.
* <p>
* Following the behavior of Lucene index managers, this
* name will reflect any annotation-based index name override
* ({@code @Indexed(index = "override")}) but will ignore
* configuration-based override
* ({@code hibernate.search.my.package.MyClass.indexName = foo}),
* which is only reflected in {@link #actualIndexName}.
*/
private String indexName;

/**
* The index name for the Elasticsearch module, i.e. the
* actual name of the underlying Elasticsearch index.
*/
private String actualIndexName;

private boolean refreshAfterWrite;
private IndexSchemaManagementStrategy schemaManagementStrategy;
private ExecutionOptions schemaManagementExecutionOptions;
Expand Down Expand Up @@ -91,7 +108,7 @@ public class ElasticsearchIndexManager implements IndexManager, IndexNameNormali
public void initialize(String indexName, Properties properties, Similarity similarity, WorkerBuildContext context) {
this.serviceManager = context.getServiceManager();

this.indexName = getIndexName( indexName, properties );
this.indexName = indexName;
this.schemaManagementStrategy = getIndexManagementStrategy( properties );
final ElasticsearchIndexStatus requiredIndexStatus = getRequiredIndexStatus( properties );
final int indexManagementWaitTimeout = getIndexManagementWaitTimeout( properties );
Expand Down Expand Up @@ -124,7 +141,8 @@ public DynamicType getDynamicMapping() {
this.schemaValidator = serviceManager.requestService( ElasticsearchSchemaValidator.class );
this.schemaTranslator = serviceManager.requestService( ElasticsearchSchemaTranslator.class );

this.actualIndexName = ElasticsearchIndexNameNormalizer.getElasticsearchIndexName( this.indexName );
String overriddenIndexName = getOverriddenIndexName( indexName, properties );
this.actualIndexName = ElasticsearchIndexNameNormalizer.getElasticsearchIndexName( overriddenIndexName );
this.refreshAfterWrite = getRefreshAfterWrite( properties );

this.similarity = similarity;
Expand All @@ -137,7 +155,7 @@ public DynamicType getDynamicMapping() {
this.requestProcessor = context.getServiceManager().requestService( BackendRequestProcessor.class );
}

private static String getIndexName(String indexName, Properties properties) {
private static String getOverriddenIndexName(String indexName, Properties properties) {
String name = properties.getProperty( Environment.INDEX_NAME_PROP_NAME );
return name != null ? name : indexName;
}
Expand Down

0 comments on commit 1b76b41

Please sign in to comment.