【发布时间】:2016-01-17 17:21:25
【问题描述】:
我有这样的映射(YAML 格式):
order:
mappings:
number: ~
createdAt:
type: date
customer:
include_in_parent: true # this is needed so we can use `customer.firstName:<term here>` on the order index
type: nested
properties:
id :
type : integer
index: not_analyzed
firstName:
type: string
index: not_analyzed
然后我尝试搜索订单并将相关客户作为聚合。因此,根据弹性搜索文档,我应该发出如下所示的请求:
http://shopblender.dev:9200/mango/order/_search
{
"size": 0,
"aggs": {
"customers": {
"nested": {
"path": "customer"
},
"aggs": {
"customer_ids": {
"terms": {
"field": "customer.id"
}
}
}
}
}
}
但是当我执行这个搜索时,它会有空聚合(我有 30 个订单被索引并且它们都有一个内联客户对象):
{
"hits": {
"total": 30,
"max_score": 0,
"hits": [
]
},
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"timed_out": false,
"aggregations": {
"customers": {
"doc_count": 30,
"customer_ids": {
"buckets": [
],
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0
}
}
},
"took": 1
}
为什么我不能在嵌套属性上使用 terms 聚合?我在这里错过了什么?
更新
这是GET /mango/order/_mapping的回复:
{
"mango": {
"mappings": {
"order": {
"_meta": {
"model": "Mango\\Component\\Core\\Model\\Order"
},
"properties": {
"createdAt": {
"type": "date",
"format": "dateOptionalTime"
},
"customer": {
"type": "nested",
"include_in_parent": true,
"properties": {
"email": {
"type": "string",
"index": "not_analyzed"
},
"firstName": {
"type": "string"
},
"id": {
"type": "integer"
},
"lastName": {
"type": "string"
}
}
},
"number": {
"type": "string"
},
"paymentState": {
"type": "string"
},
"shippingState": {
"type": "string"
},
"state": {
"type": "string"
}
}
}
}
}
}
【问题讨论】:
-
您的查询看起来不错,ES 给出零桶的唯一原因是因为它无法找到任何
id字段。还可以使用GET /mango/order/_mapping确认映射。 -
@ChintanShah25 我确实有一个客户的 id 字段映射。我在我的问题中包含了
GET /mango/order/_mapping的回复。你看到什么奇怪的东西了吗? -
"type" : "nested"在映射中丢失,这就是我猜的原因 -
@ChintanShah25 糟糕,这是错误的映射。我摆弄它。我已经更新了我的问题,这是我理解的应该起作用的映射,我还尝试使用
firstName字段等。我似乎无法在存储桶中得到任何结果。 -
我认为 ES 版本不是问题,它在 ES
1.7.2上对我有用。但我很高兴它现在正在工作