计算事件数据 Elasticsearch 聚合的转化率

Calculate conversion rate on events data Elasticsearch aggregations

在 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, ...}

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

到目前为止,我已经根据条件 uuid 汇总了大小和基数 "has_data" 并从这里继续。

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

但这是……假的。数以百万计的事件和数千个 uuids.

也没有用

我想我应该选择 scripted metric aggregation。但我不能全神贯注。有可能吗? 有人可以指出我正确的方向吗?

如果我没理解错的话,你不能只 "invert" 你发布的聚合吗?

当我创建索引("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"
               }
            }
         }
      }
   }
}

哪个returns

{
   "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

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

查看此处提供的 "entity-centric" 解决方案:https://discuss.elastic.co/t/how-can-i-use-aggregations-to-query-distinct-values-across-all-time-grouped-by-first-seen/25482