-
Notifications
You must be signed in to change notification settings - Fork 242
/
DefaultElasticsearchSchemaMigrator.java
80 lines (68 loc) · 2.71 KB
/
DefaultElasticsearchSchemaMigrator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.elasticsearch.schema.impl;
import java.util.Map;
import org.hibernate.search.elasticsearch.logging.impl.Log;
import org.hibernate.search.elasticsearch.schema.impl.model.IndexMetadata;
import org.hibernate.search.elasticsearch.schema.impl.model.TypeMapping;
import org.hibernate.search.elasticsearch.settings.impl.model.IndexSettings;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.util.logging.impl.LoggerFactory;
/**
* The default {@link ElasticsearchSchemaMigrator} implementation.
* @author Gunnar Morling
* @author Yoann Rodiere
*/
public class DefaultElasticsearchSchemaMigrator implements ElasticsearchSchemaMigrator {
private static final Log LOG = LoggerFactory.make( Log.class );
private final ElasticsearchSchemaAccessor schemaAccessor;
private final ElasticsearchSchemaValidator schemaValidator;
public DefaultElasticsearchSchemaMigrator(ElasticsearchSchemaAccessor schemaAccessor,
ElasticsearchSchemaValidator schemaValidator) {
super();
this.schemaAccessor = schemaAccessor;
this.schemaValidator = schemaValidator;
}
@Override
public void migrate(IndexMetadata indexMetadata, ExecutionOptions executionOptions) {
String indexName = indexMetadata.getName();
IndexSettings settings = indexMetadata.getSettings();
try {
/*
* We only update settings if it's really necessary, because closing the index,
* even for just a moment, may hurt if other clients are using the index.
*/
if ( !settings.isEmpty() && !schemaValidator.isSettingsValid( indexMetadata, executionOptions ) ) {
schemaAccessor.closeIndex( indexName );
try {
schemaAccessor.updateSettings( indexName, settings );
}
catch (RuntimeException mainException) {
// Try not to leave the index closed if something failed
try {
schemaAccessor.openIndex( indexName );
}
catch (RuntimeException e) {
mainException.addSuppressed( e );
}
throw mainException;
}
// Re-open the index after the settings have been successfully updated
schemaAccessor.openIndex( indexName );
}
for ( Map.Entry<String, TypeMapping> entry : indexMetadata.getMappings().entrySet() ) {
String mappingName = entry.getKey();
TypeMapping mapping = entry.getValue();
// Elasticsearch itself takes care of the actual merging
schemaAccessor.putMapping( indexName, mappingName, mapping );
}
}
catch (SearchException e) {
throw LOG.schemaUpdateFailed( indexName, e );
}
}
}