我当前正在使用ES版本2.3.5。我有一个由springboot REST项目自动创建的ES映射:
{
"customer" : {
"aliases" : { },
"mappings" : {
"customer" : {
"properties" : {
"addresses" : {
"type" : "nested",
"include_in_parent" : true,
"properties" : {
"address1" : {
"type" : "string"
},
"address2" : {
"type" : "string"
},
"address3" : {
"type" : "string"
},
"country" : {
"type" : "string"
},
"id" : {
"type" : "string"
},
"latitude" : {
"type" : "double"
},
"longitude" : {
"type" : "double"
},
"postcode" : {
"type" : "string"
},
"state" : {
"type" : "string"
},
"town" : {
"type" : "string"
},
"unit" : {
"type" : "string"
}
}
},
"companyNumber" : {
"type" : "string"
},
"contactMethods" : {
"type" : "nested",
"include_in_parent" : true,
"properties" : {
"type" : {
"type" : "string"
},
"description" : {
"type" : "string"
},
"value" : {
"type" : "string"
}
}
},
"contacts" : {
"properties" : {
"contactType" : {
"type" : "string"
},
"detail" : {
"type" : "string"
}
}
},
"id" : {
"type" : "string",
"index" : "not_analyzed"
},
"name" : {
"type" : "string"
},
"parent" : {
"type" : "nested",
"include_in_parent" : true,
"properties" : {
"id" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
"type" : {
"type" : "string"
}
}
},
"status" : {
"type" : "string"
},
"timeCreated" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
},
"timeUpdated" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
},
"type" : {
"type" : "string"
}
}
}
}
}
}
搜索服务类将构建查询。例如,通过在端点上发出“addresses.address1:Queensland”来查询地址字段,查询将是:
{
"bool" : {
"should" : {
"query_string" : {
"query" : "(addresses.address1:Queensland)",
"fields" : [ "type", "name", "companyNumber", "status", "parent.id", "parent.name", "parent.type", "addresses.id", "addresses.unit", "addresses.address1", "addresses.address2", "addresses.address3", "addresses.town", "addresses.state", "addresses.postcode", "addresses.country", "contactMethods.type", "contactMethods.value", "contactMethods.description" ],
"default_operator" : "or",
"analyze_wildcard" : true,
"lenient" : true
}
}
}
}
返回正确的文档。响应示例:
{
"page": 1,
"pageSize": 10,
"totalPages": 1,
"totalElements": 1,
"data": [
{
"id": "1",
"timeCreated": "2016-09-01T14:52:44Z",
"timeUpdated": "2016-09-01T15:25:46Z",
"type": "BUSINESS",
"name": "John Doe",
"companyNumber": "1000000002",
"status": "PENDING",
"addresses": [
{
"id": "1",
"address1": "Queensland Street",
"address2": "Casa Fuego",
"town": "New Kingslanding",
"state": "QA",
"postcode": "2222",
"country": "AU",
"longitude": 151.080739,
"latitude": -33.770029
}
],
"contactMethods": [
{
"type": "MOBILE",
"value": "0123456789"
},
{
"type": "EMAIL",
"value": "[email protected]"
}
]
}
]
}
但是,如果我通过“contactMethods.type:EMAIL”或“contactMethods.type:MOBILE”或什至“contactMethods.type:*”查询contactMethods字段,即使小写的“email”和“mobile”也返回空或0个文档。
是什么原因造成的?
最佳答案
嵌套属性被索引为不同的文档。需要使用嵌套运算符。参见https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html。