gpt4 book ai didi

elasticsearch - Elasticsearch:按嵌套对象中集合的字段上的最大值过滤

转载 作者:行者123 更新时间:2023-12-02 22:31:57 24 4
gpt4 key购买 nike

当我在ES中进行查询时,如果discounts.percent_value集合中有多个折扣(元素),我希望它仅查看discounts字段的最大值。在下面的示例中,我只希望嵌套查询对文档的折扣进行操作,并且只知道60%的折扣。

我现有的过滤器如下所示:

discount_filter = { 
nested: {
path: "discounts", query: { bool: { must: [
{ range: { "discounts.percent_value" => { lte: query_filter.discount_max } } },
{ range: { "discounts.starts_at" => { lte: "now" } } },
{ range: { "discounts.ends_at" => { gte: "now" } } }
] } },
filter: {
script: {
script: "doc['discounts.percent_value'].values.max"
}
}
}
}

映射:
"mappings": {
"product": {
"properties": {
"discounts": {
"type": "nested",
"properties": {
"ends_at": {
"type": "date",
"format": "dateOptionalTime"
},
"percent": {
"type": "string"
},
"percent_value": {
"type": "float"
},
"return_policy_key": {
"type": "string",
"index": "not_analyzed"
},
"starts_at": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
}

示例文档(具有多个折扣):
{
"_index": "products_test_index",
"_type": "product",
"_id": "6",
"_score": 1,
"_source": {
"id": 6,
"name": "Some Product",
"permalink": "some-product-42",
"price": 100,
"minimum_price": 0,
"discounts": [
{
"starts_at": "2016-01-03T01:37:02Z",
"ends_at": "2016-01-07T01:37:02Z",
"percent": "40.0",
"percent_value": "40.0",
"return_policy_key": "0.0"
},
{
"starts_at": "2016-01-03T01:37:02Z",
"ends_at": "2016-01-07T01:37:02Z",
"percent": "60.0",
"percent_value": "60.0",
"return_policy_key": "0.0"
}
]
}
}

最佳答案

我要采用的方法是仅选择嵌套的 Activity 折扣(即,开始/结束日期间隔现在包含这些折扣),然后结合使用nested + max聚合以获取最大的折扣:

{
"query": {
"nested": {
"path": "discounts",
"query": {
"bool": {
"must": [
{
"range": {
"discounts.starts_at": {
"lte": "now"
}
}
},
{
"range": {
"discounts.ends_at": {
"gte": "now"
}
}
}
]
}
}
}
},
"aggs": {
"discounts": {
"nested": {
"path": "discounts"
},
"aggs": {
"max_discounts": {
"max": {
"field": "discounts.percent_value"
}
}
}
}
}
}

关于elasticsearch - Elasticsearch:按嵌套对象中集合的字段上的最大值过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603279/

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