gpt4 book ai didi

json - 如何在不同的嵌套对象上使用术语和总和查询Elasticsearch聚合?

转载 作者:行者123 更新时间:2023-12-02 23:29:03 27 4
gpt4 key购买 nike

我有以下对象,其value属性是嵌套对象类型:

{
"metadata": {
"tenant": "home",
"timestamp": "2016-03-24T23:59:38Z"
},
"value": {
{ "key": "foo", "int_value": 100 },
{ "key": "bar", "str_value": "taco" }
}
}

这种类型的对象具有以下映射:
{
"my_index": {
"mappings": {
"my_doctype": {
"properties": {
"metadata": {
"properties": {
"tenant": {
"type": "string",
"index": "not_analyzed"
},
"timestamp": {
"type": "date",
"format": "dateOptionalTime"
}
}
},
"value": {
"type": "nested",
"properties": {
"str_value": {
"type": "string",
"index": "not_analyzed"
},
"int_value": {
"type": "long"
},
"key": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}

使用此设置,我想执行一个聚合,该聚合执行以下结果:
  • 对将term设置为str_value的对象的key属性执行"bar"聚合
  • 在通过上述聚合创建的每个存储桶中,计算sum属性的int_value,其中key设置为"foo"
  • 在给定的时间范围内以date_histogram列出结果。

  • 有了这个目标,我已经能够将 termdate_histogram聚合应用于嵌套对象,但是没有运气执行第二级计算。这是我正在尝试使用的当前查询:
    {
    "query": {
    "match_all": {}
    },
    "aggs": {
    "filters": {
    "filter": {
    "bool": {
    "must": [
    {
    "term": {
    "metadata.org": "gw"
    }
    },
    {
    "range": {
    "metadata.timestamp": {
    "gte": "2016-03-24T00:00:00.000Z",
    "lte": "2016-03-24T23:59:59.999Z"
    }
    }
    }
    ]
    }
    },
    "aggs": {
    "intervals": {
    "date_histogram": {
    "field": "metadata.timestamp",
    "interval": "1d",
    "min_doc_count": 0,
    "extended_bounds": {
    "min": "2016-03-24T00:00:00Z",
    "max": "2016-03-24T23:59:59Z"
    },
    "format": "yyyy-MM-dd'T'HH:mm:ss'Z'"
    },
    "aggs": {
    "nested_type": {
    "nested": {
    "path": "value"
    },
    "aggs": {
    "key_filter": {
    "filter": {
    "term": {
    "value.key": "bar"
    }
    },
    "aggs": {
    "groupBy": {
    "terms": {
    "field": "value.str_value"
    },
    "aggs": {
    "other_nested": {
    "reverse_nested": {
    "path": "value"
    },
    "aggs": {
    "key_filter": {
    "filter": {
    "term": {
    "value.key": "foo"
    }
    },
    "aggs": {
    "amount_sum": {
    "sum": {
    "field": "value.int_value"
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }

    我期望从Elasticsearch收到的结果如下所示:
    {
    "took": 1,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
    },
    "hits": {
    "total": 7,
    "max_score": 0.0,
    "hits": []
    },
    "aggregations": {
    "filters": {
    "doc_count": 2,
    "intervals": {
    "buckets": [
    {
    "key_as_string": "2016-03-24T00:00:00Z",
    "key": 1458777600000,
    "doc_count": 2,
    "nested_type": {
    "doc_count": 5,
    "key_filter": {
    "doc_count": 2,
    "groupBy": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
    {
    "key": "taco",
    "doc_count": 1,
    "other_nested": {
    "doc_count": 1,
    "key_filter": {
    "doc_count": 1,
    "amount_sum": {
    "value": 100.0
    }
    }
    }
    }
    ]
    }
    }
    }
    }
    ]
    }
    }
    }
    }

    但是,最里面的对象( ...groupBy.buckets.key_filter.amount_sum)的 value返回 0.0而不是 100.0

    我认为这是由于嵌套对象被索引为单独的文档这一事实,因此按一个 key属性值的过滤不允许我查询另一个 key

    有人对如何使这种查询起作用有任何想法吗?

    在更多情况下,此文档结构的原因是因为我不控制要建立索引的JSON文档的内容,因此不同的 tenant可能具有具有不同值(例如 {"tenant": "abc", "value": {"foo": "a"} }{"tenant": "xyz", "value": {"foo": 1} })的冲突键名。我尝试使用的是此 Elasticsearch Blog Post布置的对象,它建议将不受控制的对象转换为您可以使用的结构,并使用嵌套的对象来帮助解决此问题(特别是本文的 Nested fields for each data type部分)。我也乐于学习更好的方法来处理这种情况,即如果存在这样的情况,即不控制文档的JSON结构,则可以执行聚合。

    谢谢!

    编辑:我正在使用Elasticsearch 1.5。

    最佳答案

    通过以正确的方式利用reverse_nested聚合解决了这种情况,如下所述:http://www.shayne.me/blog/2015/2015-05-18-elasticsearch-nested-docs/

    关于json - 如何在不同的嵌套对象上使用术语和总和查询Elasticsearch聚合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38699762/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com