spring data elasticsearch - 使用配置文件的设置

时间:2014-05-08 07:59:01

标签: spring-data

我正在为我的应用程序和域实体

使用elasticsearch
@Document(indexName = "bookstore", type = "book", refreshInterval = "-1")
public class Book {
    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME")
    @Field(type=FieldType.String)
    private String name;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "PRICE")
    private Double price;

这是配置文件

@Configuration
@EnableElasticsearchRepositories(basePackages =
"elasticsearch.repo")
public class BookSearchRepositoryTestConfig {

@Bean

public ElasticsearchOperations elasticsearchTemplate() throws IOException {
        return new ElasticsearchTemplate(nodeBuilder()
                .loadConfigSettings(false)
                .local(true)
                .settings(
                ImmutableSettings.settingsBuilder()
                .put("index.store.type", "memory")
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 0).build()
                ).node().client());
}

此设置无效。它使用defualt设置并创建5个分片。

我知道这可以通过使用@Document

来完成
@Document(indexName = "bookstore", type = "book", shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1")

或使用@Setting

@Setting(settingPath = "/settings/elasticsearch-settings.json")

但我想使用配置文件并设置属性。 请指导我解决这个问题。

1 个答案:

答案 0 :(得分:2)

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws IOException {

 Settings settings = ImmutableSettings.settingsBuilder().loadFromClasspath("elasticsearch.yml").build();

        return new ElasticsearchTemplate(nodeBuilder()
                .loadConfigSettings(false)
                .local(true)
                .settings(settings).node().client());
    }

这很有效。但我必须将@Setting(settingPath =" /")添加到域实体。