【问题标题】:Nested aggregation does not include relevant documents in it's bucket嵌套聚合在其存储桶中不包含相关文档
【发布时间】: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 上对我有用。但我很高兴它现在正在工作

标签: elasticsearch elastica


【解决方案1】:

事实证明这是 ES 1.7.4 的问题,我已经升级到 ES 2.1 并且我的查询确实返回了预期的响应!

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2017-07-20
    • 2018-07-14
    • 2016-02-06
    • 2021-08-04
    • 2019-10-31
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    相关资源
    最近更新 更多