【发布时间】:2014-06-16 16:39:13
【问题描述】:
下面的嵌套 ElasticSearch 查询返回一些它不应该命中的结果。许多结果不包含请求的订单号,但仍然列出。我没有得到所有文档,所以查询肯定会在某种程度上减少结果集。
{
"query": {
"nested": {
"path": "orders",
"query": {
"match": {
"orderNumber": "242347"
}
}
}
}
}
查询结果(截断):
{
"took":0,
"timed_out":false,
"_shards": {
"total":1,
"successful":1,
"failed":0
},
"hits": {
"total":60,
"max_score":9.656103,
"hits":[
{
"_index": "index1",
"_type":"documenttype1",
"_id":"mUmudQrVSC6rn68ujDJ8iA",
"_score":9.656103,
"_source" : {
"documentId": 12093894,
"orders": [
{
"customerId": 129048669,
"orderNumber": "242347", // <-- CORRECT HIT ON ORDER
},
{
"customerId": 229405848,
"orderNumber": "431962"
}
]
}
},
{
"_index":"index1",
"_type":"documenttype1",
"_id":"9iO5QBCpT_6kmH3CoBTdWw",
"_score":9.656103,
"_source" : {
"documentId": 43390283,
// <-- ORDER ISN'T HERE BUT THE DOCUMENT IS HIT NEVERTHELESS!
"orders": [
{
"customerId": 229405848,
"orderNumber": "431962"
},
{
"customerId": 129408979,
"orderNumber": "142701"
}
]
}
}
// Left out 58 more results most of which do not contain
// the requested order number.
]
}
}
如您所见,有一个不应该存在的命中(实际上,其中有很多),因为没有一个订单包含请求的订单号。
这是documenttype1的映射:
{
"index1":{
"properties":{
"documentId":{
"type":"integer"
},
"orders":{
"type":"nested",
"properties":{
"customerId":{
"type":"integer"
},
"orderNumber":{
"type":"string",
"analyzer":"custom_internal_code"
}
}
}
}
}
}
最后,这里是澄清custom_internal_code 分析器的设置,如上图所示:
{
"index1":{
"settings":{
"index.analysis.analyzer.custom_internal_code.filter.1":"asciifolding",
"index.analysis.analyzer.custom_internal_code.type":"custom",
"index.analysis.analyzer.custom_internal_code.filter.0":"lowercase",
"index.analysis.analyzer.custom_internal_code.tokenizer":"keyword",
}
}
}
【问题讨论】:
-
我不知道这是否相关,但您应该将字段的完整路径放在查询中(即:orders.orderNumber)
-
@DeH:谢谢。我已经验证在查询中指定完整路径 (
orders.orderNumber) 没有任何区别。 -
您能否在您的问题中添加您在 ES 中提供的示例文档?我可能有一个解释。
-
从文档中,您必须对查询中的引用字段使用完整路径。所以
orderNumber应该变成orders.orderNumber
标签: elasticsearch