Elasticsearch总结了一天中的第一次出现

时间:2017-08-03 12:14:49

标签: elasticsearch

我正在尝试将MySQL统计信息查询转换为新的Elasticsearch(版本5.4)服务器。 在mysql中,我们有一个statistik表和第二个表,只有一天内用户的第一个请求。 目前我们正在使用PHP / MySQL来填充第二个表,并忽略statistik表中的所有其他请求。

我们在表上运行的查询如下所示:

SELECT 
    SUM(price_displayed * (query_amount / pricing_unit)) AS `requests`, 
    SUM(price_displayed * (order_amount / pricing_unit)) AS `orders`, 
    EXTRACT(YEAR_MONTH FROM `ts`) AS `date` 
FROM statistics_values

目标是摆脱第二张桌子。

是否可以只获取当天的第一个文档和用户,而不是使用脚本来计算mysql查询中的结果?

我尝试使用带有date_histogramm聚合的terms聚合,但它不起作用。

查询如下所示:

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "(client_branch:DE*)"
                    }
                },
                {
                    "range": {
                        "created": {
                            "gte": "2017-01-01",
                            "lte": "2017-05-31",
                            "format": "date"
                        }
                    }
                },
                {
                    "term": {
                        "action": "query"
                    }
                },
                {
                    "term": {
                        "type": "enquiry"
                    }
                }   
            ],
            "must_not": [
                {
                    "term": {
                        "exclude_statistics": "1"
                    }
                }
            ]
        }
    },
    "aggs": {
      "by_day": {
        "date_histogram": {
          "field": "created",
          "interval": "day"
        },
        "aggs": {
          "by_term": {
            "terms": {
              "field": "user_id"
            }
          }
        }
      }
    },
    "size": 0
}

有什么想法吗?

0 个答案:

没有答案
相关问题