I have this entity:
@Data
@Document(indexName = "foo.user")
public class ElUser {
@Id
private String id;
@Field(type = FieldType.Text)
private String fullName;
}
And this Repository:
@Repository
interface ElasticsearchUserRepository extends ElasticsearchRepository<ElUser, String> {
List<ElUser> findAllByFullNameIsLike(String name);
}
And it works fine. But I want to use a prefix like this(.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();
}
}
And remove it from entity:
@Document(indexName = "user")
But I get an exception:
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]]]
The path prefix you can configure is not a prefix for the index name like foo in foo.user, it's for the path in the URL which might be needed for some routing/proxying/dispatching software between the application and Elasticsearch.
Let's imagine you have two Elasticsearch clusters at foo:9200
and bar:9200
and there is a nginx proxy in front of them at proxy:8080
which will route an request to proxy:8080/foo/abc
to foo:9200/abc
and proxy:8080/bar/abc
to bar:9200/abc
, then you would configure you client to connect to proxy:8080
with a pathPrefix of foo.
So it's not for the use case you have.
Edit:
I have an example of how to provide an index prefix in my blog post "How to provide a dynamic index name in Spring Data Elasticsearch using SpEL", in the section Use a value from the application configuration, that might fit your needs.
_