spring data elasticsearch - 使用配置文件的设置 [英] spring data elasticsearch - settings using config file

查看:76
本文介绍了spring data elasticsearch - 使用配置文件的设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用 elasticsearch,这是域实体

I am using elasticsearch for my application and this the domain entity

@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());
}

此设置不起作用.它使用默认设置并创建 5 个分片.

This settings doesnt work.It use defualt settings and create 5 shards.

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

I know this can done by using @Document

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

或使用@Setting

or using @Setting

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

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

But I am tring to use the config file and set the properties. Please guide me to solve this issue.

推荐答案

@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="/") 添加到域实体.

This works.But I had to add @Setting(settingPath="/") to the domain entity.

这篇关于spring data elasticsearch - 使用配置文件的设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆