Spring boot + elasticsearch 添加前缀不起作用
Spring boot + elasticsearch prefix adding doen't work
我有这个实体:
@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]]]
你可以配置的路径前缀不是像foo.user中的foo这样的索引名称的前缀,它是为了URL 中某些 routing/proxying/dispatching 软件在应用程序和 Elasticsearch 之间可能需要的路径。
假设您在 foo:9200
和 bar:9200
处有两个 Elasticsearch 集群,并且在它们前面的 proxy:8080
处有一个 nginx 代理,它将请求路由到 proxy:8080/foo/abc
到 foo:9200/abc
和 proxy:8080/bar/abc
到 bar:9200/abc
,那么你将配置你的客户端连接到 proxy:8080
,路径前缀为 foo。
所以它不适用于您的用例。
编辑:
我在 blog post "How to provide a dynamic index name in Spring Data Elasticsearch using SpEL" 中有一个示例,说明如何在 使用应用程序配置中的值 部分提供索引前缀,这可能符合您的需要.
_
我有这个实体:
@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]]]
你可以配置的路径前缀不是像foo.user中的foo这样的索引名称的前缀,它是为了URL 中某些 routing/proxying/dispatching 软件在应用程序和 Elasticsearch 之间可能需要的路径。
假设您在 foo:9200
和 bar:9200
处有两个 Elasticsearch 集群,并且在它们前面的 proxy:8080
处有一个 nginx 代理,它将请求路由到 proxy:8080/foo/abc
到 foo:9200/abc
和 proxy:8080/bar/abc
到 bar:9200/abc
,那么你将配置你的客户端连接到 proxy:8080
,路径前缀为 foo。
所以它不适用于您的用例。
编辑:
我在 blog post "How to provide a dynamic index name in Spring Data Elasticsearch using SpEL" 中有一个示例,说明如何在 使用应用程序配置中的值 部分提供索引前缀,这可能符合您的需要.
_