FieldNamingStrategy 与 Spring 数据 Elasticsearch
FieldNamingStrategy with Spring Data Elasticsearch
我正在配置一个 elasticsearch spring 应用程序,并按照文档创建了这个 RestHighLevelClient:
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
现在我希望我的所有文档都具有 snake_case 作为命名策略,并且在文档中这是我发现的:
Without further configuration, Spring Data Elasticsearch will use the property name of an object as field name in Elasticsearch. This can be changed for individual field by using the @Field annotation on that property.
It is also possible to define a FieldNamingStrategy in the configuration of the client (Elasticsearch Clients). If for example a SnakeCaseFieldNamingStrategy is configured, the property sampleProperty of the object would be mapped to sample_property in Elasticsearch. A FieldNamingStrategy applies to all entities; it can be overwritten by setting a specific name with @Field on a property.
问题是,如何一次配置客户端?
只需覆盖配置中的 fieldNamingStrategy()
方法 class:
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Override
protected FieldNamingStrategy fieldNamingStrategy() {
return new SnakeCaseFieldNamingStrategy();
}
}
我正在配置一个 elasticsearch spring 应用程序,并按照文档创建了这个 RestHighLevelClient:
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
现在我希望我的所有文档都具有 snake_case 作为命名策略,并且在文档中这是我发现的:
Without further configuration, Spring Data Elasticsearch will use the property name of an object as field name in Elasticsearch. This can be changed for individual field by using the @Field annotation on that property.
It is also possible to define a FieldNamingStrategy in the configuration of the client (Elasticsearch Clients). If for example a SnakeCaseFieldNamingStrategy is configured, the property sampleProperty of the object would be mapped to sample_property in Elasticsearch. A FieldNamingStrategy applies to all entities; it can be overwritten by setting a specific name with @Field on a property.
问题是,如何一次配置客户端?
只需覆盖配置中的 fieldNamingStrategy()
方法 class:
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Override
protected FieldNamingStrategy fieldNamingStrategy() {
return new SnakeCaseFieldNamingStrategy();
}
}