【问题标题】:Calculate conversion rate on events data Elasticsearch aggregations计算事件数据 Elasticsearch 聚合的转化率
【发布时间】:2015-10-27 23:19:17
【问题描述】:

有没有一种简单的方法可以通过 elasticsearch 中的聚合计算转化率?

我有一些事件数据,例如:

{"uuid": "a92405ef-9632-44ce-9cb3-0ae83e434fe9", 
 "created_at": "2015-10-26T21:58:23.132923+00:00",
 "has_data": true, ...}

{"uuid": "4a342de5-4047-4897-8f30-f60c64def839", 
 "created_at": "2015-10-26T21:57:43.985108+00:00",
 "has_data": true, ...}

{"uuid": "47d6add8-003d-4c67-8e9f-1712999b4f15", 
 "created_at": "2015-10-26T21:51:11.062669+00:00",
 "has_data": false, ...}

{"uuid": "a92405ef-9632-44ce-9cb3-0ae83e434fe9", 
 "created_at": "2015-10-26T21:44:17.121071+00:00",
 "has_data": false, ...}

我需要计算 has_data 标志设置为 true 但之前(及时,在另一个文档中)设置为的 uuid 的唯一计数false 或相反。 对于上面的例子,我的预期结果应该是 1。只有“a92405ef-9632-44ce-9cb3-0ae83e434fe9”在两个文档中,并且同时具有 truefalse “has_data”。

到目前为止,我已经根据“has_data”的大小和基数汇总了 uuid 条款,并从这里开始。

"aggs": {
  "2": {
    "terms": {
      "field": "uuid",
      "size": 0,
    },
    "aggs": {
      "1": {
        "cardinality": {
          "field": "has_data"
        }
      }
    }
  }
}

但这是……假的。对数百万个事件和数千个 uuids 没有好处。

我想我应该选择scripted metric aggregation。但我不能把头包起来。有可能吗? 有人能指出我正确的方向吗?

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    您的问题包含我们称之为“桶爆炸”的问题的成分 - 请参阅http://www.slideshare.net/NoSQLmatters/entity-centric-indexing-no-sql-dublin#5

    查看此处介绍的“以实体为中心”的解决方案:https://discuss.elastic.co/t/how-can-i-use-aggregations-to-query-distinct-values-across-all-time-grouped-by-first-seen/25482

    【讨论】:

    • 我一直在考虑类似于“以实体为中心”的解决方案,但我无法命名。
    【解决方案2】:

    如果我对您的理解正确,您就不能“反转”您发布的聚合吗?

    当我创建一个索引(将"uuid" 设置为"index":"not_analyzed")并添加您发布的数据时,我可以运行此聚合:

    POST /test_index/_search?search_type=count
    {
       "aggs": {
          "has_data_terms": {
             "terms": {
                "field": "has_data"
             },
             "aggs": {
                "has_data_card": {
                   "cardinality": {
                      "field": "uuid"
                   }
                }
             }
          }
       }
    }
    

    返回

    {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 1,
          "successful": 1,
          "failed": 0
       },
       "hits": {
          "total": 4,
          "max_score": 0,
          "hits": []
       },
       "aggregations": {
          "has_data_terms": {
             "doc_count_error_upper_bound": 0,
             "sum_other_doc_count": 0,
             "buckets": [
                {
                   "key": "F",
                   "doc_count": 2,
                   "has_data_card": {
                      "value": 2
                   }
                },
                {
                   "key": "T",
                   "doc_count": 2,
                   "has_data_card": {
                      "value": 2
                   }
                }
             ]
          }
       }
    }
    

    所以忽略"key": "F""key": "T" 应该会给你你想要的计数。然后得到一个完整的uuid 计数,你应该能够计算出你想要的比例。将这种技术专门用于特定时间段应该很简单。

    这是我用来测试它的代码:

    http://sense.qbox.io/gist/993546914daf15e88ac3e1095a9dfed775b0741c

    【讨论】:

    • 感谢您的回复。我已经稍微更新了这个问题。我对预期值不太清楚。对于示例数据,我的预期结果应该是 1。只有“a92405ef-9632-44ce-9cb3-0ae83e434fe9”在两个文档中,并且有真假作为“has_data”。
    • 啊,明白了。您可以重新组织数据的保存方式吗?如果您愿意使用父/子关系,我想我知道如何设置它。
    • 我无法更新结构,但可以预处理数据。 “以实体为中心”的解决方案似乎是一个不错的起点。
    猜你喜欢
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2016-08-21
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2020-07-11
    相关资源
    最近更新 更多