Elasticsearch:date_range存储桶的操作总和

时间:2017-01-04 14:49:13

标签: elasticsearch date-range

我有一些与ES(1.7)有关的事情,我不知道该怎么做。 让我看看我是否可以用sql和一个表来显示它,也许它更容易转换为ES查询。 想象一下表points_by_date

DATE    PURCHASE_COUNT  ACCUMULATED_POINTS
day 1         5              548
day 2         8              498
day 3         9              623
day 4         9              635
day 5        13              620

如果它是一个表,我会做这样的查询来检索我需要的东西:

SELECT SUM(q.AVG_POINT_PURCHASE) as POINT_BY_PURCHASE
FROM (
   SELECT (ACCUMULATED_POINTS / PURCHASE_COUNT) as AVG_POINT_PURCHASE
   FROM points_by_date
) q;

是否可以在ES中执行此操作?我需要补充说,这些操作是在日期范围的桶上完成的,如下所示:

{
  "timeout": 1500,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "from": "2016-12-01",
              "to": "2016-12-05",
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "my_ranges": {
      "date_range": {
        "field": "date",
        "ranges": [
          {
            "from": "2016-12-01",
            "to": "2016-12-05"
          },
          {
            "from": "2016-12-06",
            "to": "2016-12-10"
          }
        ]
      },
      "aggregations": {
        "TOTAL_POINTS" : {
          "sum" : {
            "field" : "ACCUMULATED_POINTS"
          }
        },
        "PURCHASE_COUNT" : {
          "avg" : {
            "field" : "PURCHASE_COUNT"
          }
        }
      }
    }
  }
}

每个日期范围代表表格中的一组N行,从中我需要进行数学运算。

ES查询具有先前的方法,其中我获得每桶的平均purchase_count,并且稍后将桶的cumulative_points的总和除以该平均值。现在我需要先划分,然后按桶累积。如有可能,怎么想?

提前致谢!

1 个答案:

答案 0 :(得分:0)

万一有人需要它或类似的东西,我设法做到了:

{
  "timeout": 1500,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "from": "2016-12-01",
              "to": "2016-12-05",
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "my_ranges": {
      "date_range": {
        "field": "date",
        "ranges": [
          {
            "from": "2016-12-01",
            "to": "2016-12-05"
          },
          {
            "from": "2016-12-06",
            "to": "2016-12-10"
          }
        ]
      },
      "aggregations": {
        "sum_avg_purchase_point": {
          "sum": {
            "script": {
              "inline": "doc[accumulatedPoints].value.intValue() / (doc[purchaseCount].value.intValue() == 0 ? 1 : doc[purchaseCount].value.intValue())",
              "params": {
                "accumulatedPoints": "ACCUMULATED_POINTS",
                "purchaseCount": "PURCHASE_COUNT"
              }
            }
          }
        }
      }
    }
  }
}

我首先尝试了针对ES的所有内容,一旦我开始工作并完成了我需要的微积分,并且在验证结果后,我使用Java API创建了该查询。

希望它有所帮助!此致!!!