弹性搜索高级查询,用于显示时间序列指数的价格变化



我已经将我的时间序列数据分成了按天的索引。假设我有 2 天的索引。

1( 索引1 数据时间序列-14-04-2019

示例数据:

{{"_id": 1,
  "product_name": "mobile1",
  "product_id": "12145",
  "created_at": "2014-04-14",
  "offer_price": 89
  },
 {"_id": 2,
  "product_name": "mobile2",
  "product_id": "12146",
  "created_at": "2014-04-14",
  "offer_price": 70
  }
 }

2( 索引2 数据-时间序列-15-04-2019

{{"_id": 1,
  "product_name": "mobile2",
  "product_id": "12146",
  "created_at": "2014-04-15",
  "offer_price": 80
  },
 {"_id": 2,
  "product_name": "mobile1",
  "product_id": "12145",
  "created_at": "2014-04-15",
  "offer_price": 89
  }
 }
`

在这里,独特的产品通过其"product_id"来识别

我想写一个查询,显示产品的数量,这些产品已经改变了offer_price。

请帮帮我。

我尝试过这样的事情

{
    "aggs": {
        "distinct_by_product_id": {
            "terms": {
                "field": "product_id.keyword",
                "min_doc_count": 2,
                "size": 10
            },
            "aggs": {"count_distinct_prices":
                {"cardinality": {
                    "field": "offer_price"
                }}
            }
        }}
}

首先对product_id应用术语聚合,然后找到非重复offer_price。如果非重复数 no 大于 1,则该产品计为价格变体产品。

  • 但是由于我使用的是术语聚合,对于大量数据,我无法获得所有产品数据。

您可以增加 size 参数以更改返回的不同产品的数量,但这可能会导致性能问题。

最新更新