ElasticSearch - 计算聚合桶之间的比率

时间:2016-01-17 14:47:26

标签: elasticsearch

我使用以下术语聚合来获取每个广告系列的观看次数和点击次数(按campaign_id):

"aggregations": {
"campaigns": {
        "terms": {
    "field": "campaign_id",
    "size": 10,
    "order": {
      "_term": "asc"
    }
  },
  "aggregations": {
    "actions": {
      "terms": {
        "field": "action",
        "size": 10
      }
    }
  }
}}

这是我得到的回复:

   "aggregations": {
  "campaigns": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "someId",
           "doc_count": 12,
           "actions": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                 {
                    "key": "click",
                    "doc_count": 3
                 },
                 {
                    "key": "view",
                    "doc_count": 9
                 }
              ]
           }
        }
     ]
  }

}

修改 这是一个文档的例子(只有它的相关部分......):

{
"_index": "action",
"_type": "click",
"_id": "AVI2XOTl8otXlszOjypT",
"_score": 1,
"_source": {
    "ip": "127.0.0.1",
    "timestamp": "2016-01-12T15:03:23.622743524Z",
    "action": "click",
    "campaign_id": "IypmiroC"
}}

我需要能够检索每个广告系列的转化率(点击次数/观看次数),而我无法在客户端进行,因为我需要能够按转化率进行排序。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

这需要使用各种aggregationsES 2.x。首先,我使用terms aggregation获取所有唯一的campaign_id。然后我filtering actions并获取count个特定操作的文档。然后,您需要使用pipeline aggregation中引入的ES 2.0,主要是bucket script aggregation来获取该比率。这就是它的外观。

{
  "size": 0,
  "aggs": {
    "unique_campaign": {
      "terms": {
        "field": "campaign_id",
        "size": 10
      },
      "aggs": {
        "click_bucket": {
          "filter": {
            "term": {
              "action": "click"
            }
          },
          "aggs": {
            "click_count": {
              "value_count": {
                "field": "action"
              }
            }
          }
        },
        "view_bucket": {
          "filter": {
            "term": {
              "action": "view"
            }
          },
          "aggs": {
            "view_count": {
              "value_count": {
                "field": "action"
              }
            }
          }
        },
        "conversion_ratio": {
          "bucket_script": {
            "buckets_path": {
              "total_clicks": "click_bucket>click_count",
              "total_views": "view_bucket>view_count"
            },
            "script": "total_clicks/total_views"
          }
        }
      }
    }
  }
}

此外,您需要not_analyzed的{​​{1}}映射为点击不匹配点击

希望这会有所帮助!!

答案 1 :(得分:0)

到目前为止,7.xsorting可以通过以下方式实现,仅是供参考的演示:

{
  “size”: 0,
  “aggs”: {
    “mallBucket”: {
      “terms”: {
        “field”: “mallId”,
        “size”: 20,
        “min_doc_count”: 3,
        “shard_size”: 10000
      },
      “aggs”: {
        “totalOrderCount”: {
          “value_count”: {
            “field”: “orderSn”
          }
        },
        “filteredCoupon”: {
          “filter”: {
            “terms”: {
              “tags”: [
                “hello”,
                "cool”
              ]
            }
          },
          “aggs”: {
            “couponCount”: {
              “value_count”: {
                “field”: “orderSn”
              }
            }
          }
        },
        “countRatio”: {
          “bucket_script”: {
            “buckets_path”: {
              “orderCount”: “totalOrderCount”,
              “couponCount”: “filteredCoupon>couponCount”
            },
            “script”: “params.couponCount/params.orderCount”
          }
        },
        “ratio_bucket_sort”: {
          “bucket_sort”: {
            “sort”: [
              {
                “countRatio”: {
                  “order”: “desc”
                }
              }
            ],
            “size”: 20
          }
        }
      }
    }
  }
}