我有一个关于Spring数据elasticSearch的项目。
模型:
@Document(indexName = "address", createIndex = true)
public class Address {
@Id
private String id;
@Field(type = FieldType.Text )
private String fullAddress;
private String regionCode;
@Field(type = FieldType.Nested, store = true)
private List<Entry> parts;
public Address(String fullAddress) {
this.fullAddress = fullAddress;
}
public Address(String fullAddress, List<Entry> entryList) {
this.fullAddress = fullAddress;
this.parts = entryList;
}
public Address(String fullAddress, List<Entry> entryList, String regionCode) {
this.fullAddress = fullAddress;
this.parts = entryList;
this.regionCode = regionCode;
}
}
存储库(fullAddress ==“*俄罗斯莫斯科*”;如果没有@Query,则该方法不适用于包含空格的键)
@Repository
public interface AddressElasticRepository extends ElasticsearchRepository<Address, String> {
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"fullAddress\" : {\"query\" : \"?\",\"analyze_wildcard\" : true}}}}}")
List<Address> findByFullAddressLike(String fullAddress);
}
我在Elastic中的索引:
{
"address": {
"aliases": {},
"mappings": {
"properties": {
"fullAddress": {
"type": "text"
},
"parts": {
"type": "nested",
"properties": {
"aoGuid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"aoLevel": {
"type": "integer"
},
"aoid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"code": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"offName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"parentGuid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"postalCode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"shortName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"regionCode": {
"type": "integer"
}
}
},
"settings": {
"index": {
"refresh_interval": "1s",
"number_of_shards": "1",
"provided_name": "address",
"creation_date": "1582120325272",
"store": {
"type": "fs"
},
"number_of_replicas": "1",
"uuid": "CG3m-SDdT9CqpXXJ2knl4g",
"version": {
"created": "7040099"
}
}
}
}
}
当我调用findByFullAddressLike方法时,出现错误:
我该怎么办才能使错误消失?
最佳答案
错误状态
您的查询不正确(即field
不是有效的查询),应该是
@Query("{\"bool\":{\"must\":{\"query_string\":{\"query\":\"?\",\"default_field\":\"fullAddress\",\"analyze_wildcard\":true}}}}")