If your application only requires a single index, you can use the SingleIndexElasticsearchService
to create and delete the index:
@Autowired
private SingleIndexElasticsearchService singleIndexElasticsearchService;
private void createIndex() {
singleIndexElasticsearchService.createIndexWithAlias();
}
private void deleteIndex() {
singleIndexElasticsearchService.deleteIndexWithAlias();
}
When using the SingleIndexElasticsearchService
and the DefaultElasticConfiguration
, the index name and index alias are defined by
elasticsearch.properties
:
Default configuration
@Configuration
@ComponentScan(basePackages = {"de.picturesafe.search.elasticsearch"})
@Import({DefaultElasticConfiguration.class})
// same as @Import({DefaultClientConfiguration.class,
// DefaultIndexConfiguration.class,
// DefaultQueryConfiguration.class})
public class Config {
@Bean
List<FieldConfiguration> fieldConfigurations() { ... }
}
elasticsearch.properties
#elasticsearch.index.alias=default
## Index name prefix (property 'elasticsearch.index.alias' will be used when empty)
#elasticsearch.index.name_prefix=
## Index name date format ('yyyyMMdd-HHmmss-SSS' will be used when empty)
#elasticsearch.index.name_date_format=yyyyMMdd-HHmmss-SSS
With the above settings an index with the name ‘default-yyyyMMdd-HHmmss-SSS
’ and the alias ‘default’ is created.
Created index with alias in Cerebro
If your application requires more than one index, you have to use the ElasticsearchService
instead of the SingleIndexElasticsearchService
to create and delete the index.
To create and delete an index, the corresponding index alias must be passed:
@Autowired
private ElasticsearchService elasticsearchService;
private void createIndex(String indexAlias) {
elasticsearchService.createIndexWithAlias(indexAlias);
}
private void deleteIndex(String indexAlias) {
singleIndexElasticsearchService.deleteIndexWithAlias(indexAlias);
}
For simple use cases the DefaultClientConfiguration
and the DefaultQueryConfiguration
can be imported. Some beans for the index and field configuration have
to be implemented additionally:
Configuration with customized index configuration for two indices
@Configuration
@ComponentScan(basePackages = {"de.picturesafe.search.elasticsearch"})
@Import({DefaultClientConfiguration.class, DefaultQueryConfiguration.class})
public class Config {
public static final String FIRST_INDEX_ALIAS = "myfirstindex";
public static final String SECOND_INDEX_ALIAS = "mysecondindex";
@Value("${elasticsearch.index.my_first_index.alias:" + FIRST_INDEX_ALIAS + "}")
private String myFirstIndexAlias;
@Value("${elasticsearch.index.my_second_index.alias:" + SECOND_INDEX_ALIAS + "}")
private String mySecondIndexAlias;
@Value("${elasticsearch.index.my_first_index.name_prefix:#{null}}")
private String myFirstIndexNamePrefix;
@Value("${elasticsearch.index.my_second_index.name_prefix:#{null}}")
private String mySecondIndexNamePrefix;
@Value("${elasticsearch.index.name_date_format:yyyyMMdd-HHmmss-SSS}")
private String indexNameDateFormat;
@Value("${elasticsearch.index.number_of_shards:1}")
private int numberOfShards;
@Value("${elasticsearch.index.number_of_replicas:0}")
private int numberOfReplicas;
@Value("${elasticsearch.index.fields_limit:1000}")
private int fieldsLimit;
@Value("${elasticsearch.index.max_result_window:10000}")
private int maxResultWindow;
/*
// Before 3.3.0
@Bean
List<IndexPresetConfiguration> indexPresetConfiguration() {
return Arrays.asList(
getIndexPresetConfiguration(myFirstIndexAlias, myFirstIndexNamePrefix),
getIndexPresetConfiguration(mySecondIndexAlias, mySecondIndexNamePrefix)
);
}
*/
// since 3.3.0
@Bean
IndexPresetConfigurationProvider indexPresetConfigurationProvider() {
return new StaticIndexPresetConfigurationProvider(Arrays.asList(
getIndexPresetConfiguration(myFirstIndexAlias, myFirstIndexNamePrefix),
getIndexPresetConfiguration(mySecondIndexAlias, mySecondIndexNamePrefix)
));
}
@Bean
FieldConfigurationProvider fieldConfigurationProvider() {
final Map<String, List<FieldConfiguration>> fieldConfigurationMap
= new HashMap<>();
fieldConfigurationMap
.put(myFirstIndexAlias, firstIndexfieldConfigurations());
fieldConfigurationMap
.put(mySecondIndexAlias, secondIndexfieldConfigurations());
return new StaticFieldConfigurationProvider(fieldConfigurationMap);
}
private List<FieldConfiguration> firstIndexfieldConfigurations() { ... }
private List<FieldConfiguration> secondIndexfieldConfigurations() { ... }
private IndexPresetConfiguration
getIndexPresetConfiguration(String indexAlias,
String indexNamePrefix) {
final StandardIndexPresetConfiguration cfg
= new StandardIndexPresetConfiguration(indexAlias, indexNamePrefix,
indexNameDateFormat, numberOfShards, numberOfReplicas, maxResultWindow);
cfg.setFieldsLimit(fieldsLimit);
cfg.setCharMappings(defaultCharMapping());
return cfg;
}
// Optional char mapping
private Map<String, String> defaultCharMapping() {
final Map<String, String> charMapping = new HashMap<>();
charMapping.put("ä", "ae");
charMapping.put("ö", "oe");
charMapping.put("ü", "ue");
charMapping.put("ß", "ss");
charMapping.put("Ä", "Ae");
charMapping.put("Ö", "Oe");
charMapping.put("Ü", "Ue");
return charMapping;
}
}
Created indices with alias for above settings in Cerebro
with by the picturesafe-search community
Code licensed Apache License 2.0 Documentation licensed CC-BY-4.0