Need to write a custom analyzer for ElasticsearchRepository findBy query

我们两清 提交于 2020-04-18 03:49:23

问题


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

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.


回答1:


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.

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.

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.

See the Elasticsearch docs how such a query is built and Spring Data Elasticsearch docs for the use of the @Query annotation.



来源:https://stackoverflow.com/questions/60857245/need-to-write-a-custom-analyzer-for-elasticsearchrepository-findby-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!