问题描述
我有一个具有以下文档架构的ES集合.
I have an ES collection with following doc schema.
`
public class Address {
@Id
private String id;
private String name;
private String type;
private String city;
}
`我的存储库如下所示:
`My repository looks like as follows:
public interface NetworkElementsESRepository extends ElasticsearchRepository<Address, String> {
Address findByNameAndCity(String name, String city);}
我需要使用以下查询来获取特定城市中名称为"B00/A3K/24"的所有地址. addressRepo.findByNameAndCity(Name,City).我无法获取所需的地址,因为 name 字段具有特殊字符('/'),标准的Analyzer似乎将搜索字符串分解为不同的标记.我需要有关ElasticsearchRepository的自定义分析器的帮助.
I need to fetch all addresses with name= "B00/A3K/24" in a particular city using following Query.addressRepo.findByNameAndCity(Name,City) . I am not able to fetch the required addresses since name field has special characters ('/') and standard Analyzer seems to break the search string into different token. I need help with the custom Analyzer for ElasticsearchRepository.
推荐答案
First you need to either create the analyzer in Elasticsearch yourself, or by providing a settings file when creating the index with Spring Data Elasticsearch.
如果您始终需要此字段的自定义分析器,则可以在 @Field
批注中指定分析器,您可以使用 analyzer
或使用 searchAnalyzer
.
If you always need the custom analyzer for this field you can specify the analyzer in the @Field
annotation you put on the name
property either with analyzer
or with searchAnalyzer
.
如果仅需要使用分析器进行存储库搜索,则应创建一个以 @Query
注释的存储库方法,然后在其中添加自定义查询.
If you only need the analyzer for the search with the repository, you should create a repository method annotated with @Query
and add your custom query there.
请参见 Elasticsearch文档如何构建这样的查询,并 Spring Data Elasticsearch文档,用于使用 @Query
批注.
See the Elasticsearch docs how such a query is built and Spring Data Elasticsearch docs for the use of the @Query
annotation.
这篇关于需要为ElasticsearchRepository findBy查询编写自定义分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!