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

时间:2019-06-15 04:57:21

标签: elasticsearch elastic-stack amazon-elastic-beanstalk elasticsearch-painless

我已将时间序列数据分为按日分类的索引。 假设我有2天的索引。

1)index1 数据时间序列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)index2 data-timeseries-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上应用术语汇总,然后找到不同的no offer_price。如果不重复编号大于1,则将该产品计为价格变动产品。

  • 但是当我使用术语汇总时,对于大量数据,我无法获取所有产品数据。

1 个答案:

答案 0 :(得分:0)

您可以增加function PostCode(codestring) { return new Promise((resolve, reject) => { // Build the post string from an object var post_data = querystring.stringify({ 'compilation_level': 'ADVANCED_OPTIMIZATIONS', 'output_format': 'json', 'output_info': 'compiled_code', 'warning_level': 'QUIET', 'js_code': codestring }); // An object of options to indicate where to post to var post_options = { host: 'closure-compiler.appspot.com', port: '80', path: '/compile', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(post_data) } }; //Store chunks var response = ""; // Set up the request var post_req = http.request(post_options, function(res) { res.setEncoding('utf8'); res.on('data', function(chunk) { response += chunk; console.log('Response: ' + chunk); }); res.on('end', function() { //resolve on end event resolve(response); }); res.on('error', function(error) { //reject on error event reject(error); }); }); // post the data post_req.write(post_data); post_req.end(); }); }参数来更改返回的不同产品的数量,但这可能会导致性能问题。