问题
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