【问题标题】:Spring boot + elasticsearch prefix adding doen't workSpring boot + elasticsearch前缀添加不起作用
【发布时间】:2020-11-23 11:41:12
【问题描述】:

我有这个实体:

@Data
@Document(indexName = "foo.user")
public class ElUser {

    @Id
    private  String id;

    @Field(type = FieldType.Text)
    private String fullName;
}

还有这个存储库:

@Repository
interface ElasticsearchUserRepository extends ElasticsearchRepository<ElUser, String> {
List<ElUser> findAllByFullNameIsLike(String name);
}

而且效果很好。但我想使用这样的前缀(.withPathPrefix("foo")):

@EnableElasticsearchRepositories
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {

        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .withPathPrefix("foo")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }
}

并将其从实体中移除:

@Document(indexName = "user")

但我得到一个例外:

org.springframework.data.elasticsearch.NoSuchIndexException: Index [foo] not found.; nested exception is [foo] ElasticsearchStatusException[Elasticsearch exception [type=index_not_found_exception, reason=no such index [foo]]]

【问题讨论】:

    标签: spring spring-boot elasticsearch spring-data-elasticsearch


    【解决方案1】:

    您可以配置的路径前缀不是foo.user中的foo之类的索引名称的前缀,它是URL中可能需要的路径应用程序和 Elasticsearch 之间的一些路由/代理/调度软件。

    假设您在 foo:9200bar:9200 有两个 Elasticsearch 集群,在它们前面有一个位于 proxy:8080 的 nginx 代理,它将把请求路由到 proxy:8080/foo/abcfoo:9200/abcproxy:8080/bar/abcbar:9200/abc,那么你可以配置你的客户端连接到proxy:8080,路径前缀为foo

    所以它不适合你的用例。

    编辑:

    在我的blog post "How to provide a dynamic index name in Spring Data Elasticsearch using SpEL" 中,我有一个示例说明如何在使用应用程序配置中的值 部分中提供索引前缀,这可能符合您的需求。

    _

    【讨论】:

    • 好的,谢谢!你知道我该怎么做我想做的事吗?从@Document(indexName = "foo.user") 中提取 foo 并将其保存在属性中
    • 感谢您的回答!但解决方案是拐杖。我需要在每个@Document 中添加 SpEL 语句。有人可能会错过它。重构呢?而且我有奇怪的行为 - @Document(indexName = "#{@appPropertiesService.getElasticsearchProperties().getIndexName()}", createIndex = false) 抛出异常 Consider defining a bean named 'appPropertiesService' in your configuration. 但使用实现工作正常 - @Document(indexName = "#{@defaultAppPropertiesService.getElasticsearchProperties().getIndexName()}", createIndex = false)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多