Spring 具有动态文档副本和分片的数据弹性搜索
Spring Data Elastic Search with dynamic document replicas and shards
我正在使用 spring boot 2.4 和 spring-data-elasticsearch 4.1。我有这样的文档
@Document(indexName = "test", replicas = 3, shards = 2)
public class TestDocument {
@Id
@Field(type = FieldType.Keyword)
private String Id;
@Field(type = FieldType.Object, enabled = false)
private String name;
...
getters
setters
}
而且我想覆盖来自 application.yml 的文档注释中的副本和分片中的硬编码值以创建索引,因为这些值可能因我的服务环境而异。有什么办法吗?
您可以使用
禁用索引的自动创建
@Document(indexName = "test", createIndex = false)
然后使用IndexOperations.create(settings)
创建索引。以下代码摘自 Spring Data Elasticsearch 测试(https://github.com/spring-projects/spring-data-elasticsearch/blob/4.1.x/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java#L2361-L2384):
public void shouldCreateIndexWithGivenClassAndSettings() {
// given
String settings = "{\n" + " \"index\": {\n" + " \"number_of_shards\": \"1\",\n"
+ " \"number_of_replicas\": \"0\",\n" + " \"analysis\": {\n"
+ " \"analyzer\": {\n" + " \"emailAnalyzer\": {\n"
+ " \"type\": \"custom\",\n"
+ " \"tokenizer\": \"uax_url_email\"\n" + " }\n"
+ " }\n" + " }\n" + " }\n" + '}';
// when
indexOperations.delete();
indexOperations.create(parse(settings));
indexOperations.putMapping(SampleEntity.class);
indexOperations.refresh();
// then
Map<String, Object> map = indexOperations.getSettings();
assertThat(operations.indexOps(IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY)).exists()).isTrue();
assertThat(map.containsKey("index.number_of_replicas")).isTrue();
assertThat(map.containsKey("index.number_of_shards")).isTrue();
assertThat((String) map.get("index.number_of_replicas")).isEqualTo("0");
assertThat((String) map.get("index.number_of_shards")).isEqualTo("1");
}
您应该在代码中创建 Document
,而不是为设置解析 JSON:
Document settings = Document.create();
settings.put("index.number_of_replicas", 42);
settings.put("index.number_of_shards", 42);
我正在使用 spring boot 2.4 和 spring-data-elasticsearch 4.1。我有这样的文档
@Document(indexName = "test", replicas = 3, shards = 2)
public class TestDocument {
@Id
@Field(type = FieldType.Keyword)
private String Id;
@Field(type = FieldType.Object, enabled = false)
private String name;
...
getters
setters
}
而且我想覆盖来自 application.yml 的文档注释中的副本和分片中的硬编码值以创建索引,因为这些值可能因我的服务环境而异。有什么办法吗?
您可以使用
禁用索引的自动创建@Document(indexName = "test", createIndex = false)
然后使用IndexOperations.create(settings)
创建索引。以下代码摘自 Spring Data Elasticsearch 测试(https://github.com/spring-projects/spring-data-elasticsearch/blob/4.1.x/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java#L2361-L2384):
public void shouldCreateIndexWithGivenClassAndSettings() {
// given
String settings = "{\n" + " \"index\": {\n" + " \"number_of_shards\": \"1\",\n"
+ " \"number_of_replicas\": \"0\",\n" + " \"analysis\": {\n"
+ " \"analyzer\": {\n" + " \"emailAnalyzer\": {\n"
+ " \"type\": \"custom\",\n"
+ " \"tokenizer\": \"uax_url_email\"\n" + " }\n"
+ " }\n" + " }\n" + " }\n" + '}';
// when
indexOperations.delete();
indexOperations.create(parse(settings));
indexOperations.putMapping(SampleEntity.class);
indexOperations.refresh();
// then
Map<String, Object> map = indexOperations.getSettings();
assertThat(operations.indexOps(IndexCoordinates.of(INDEX_NAME_SAMPLE_ENTITY)).exists()).isTrue();
assertThat(map.containsKey("index.number_of_replicas")).isTrue();
assertThat(map.containsKey("index.number_of_shards")).isTrue();
assertThat((String) map.get("index.number_of_replicas")).isEqualTo("0");
assertThat((String) map.get("index.number_of_shards")).isEqualTo("1");
}
您应该在代码中创建 Document
,而不是为设置解析 JSON:
Document settings = Document.create();
settings.put("index.number_of_replicas", 42);
settings.put("index.number_of_shards", 42);