我正在尝试使用 spring-data-elasticsearch 为与下面的 json 配置等效的类型设置映射,但我遇到了问题。
{
"_id" : {
"type" : "string",
"path" : "id"
},
"properties":{
"addressName":{
"type":"multi_field",
"fields":{
"addressName":{
"type":"string"
},
"edge":{
"analyzer":"edge_search_analyzer",
"type":"string"
}
}
},
"companyName":{
"type":"multi_field",
"fields":{
"companyName":{
"type":"string"
},
"edge":{
"analyzer":"edge_search_analyzer",
"type":"string"
}
}
}
}
}
这是现在的实体类:
@Document(indexName = "test", type="address")
@Setting(settingPath="/config/elasticsearch/test.json") // Used to configure custom analyzer/filter
public class Address {
@Id
private Integer id;
@MultiField(
mainField = @Field(type = FieldType.String),
otherFields = {
@NestedField(dotSuffix = "edge", type = FieldType.String, indexAnalyzer = "edge_search_analyzer")
}
)
private String addressName;
@MultiField(
mainField = @Field(type = FieldType.String),
otherFields = {
@NestedField(dotSuffix = "edge", type = FieldType.String, indexAnalyzer = "edge_search_analyzer")
}
)
private String companyName;
public Address() {}
public Address(AddressDTO dto) {
id = dto.getId();
addressName = dto.getAddressName();
companyName = dto.getCompanyName();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
Preconditions.checkNotNull(id);
this.id = id;
}
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
我发现这会导致如下映射:
"address" : {
"properties" : {
"addressName" : {
"type" : "string",
"fields" : {
"addressName.edge" : {
"type" : "string",
"index_analyzer" : "edge_search_analyzer"
}
}
},
"companyName" : {
"type" : "string",
"fields" : {
"companyName.edge" : {
"type" : "string",
"index_analyzer" : "edge_search_analyzer"
}
}
}
}
}
这导致两个问题:
- id 映射未完成,因此 Elasticsearch 生成 id
- 使用自定义分析器搜索属性无法正常工作
如果我添加以下内容来覆盖注释的使用,一切正常:
@Mapping(mappingPath="/config/elasticsearch/address.json")
其中“address.json”包含本文顶部的映射配置。
那么,任何人都可以指出我想要实现的目标可能会出错的地方吗?是否可以使用可用的注释或者我将不得不坚持使用 json 配置?
此外,是否有通过 spring-data-elasticsearch 设置索引级配置的“正确”方法,或者这不是受支持的方法?目前,我为此目的使用@Setting 注释