我正在使用以下术语聚合来获取每个广告系列的观看次数和点击次数(通过 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"
}}

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

任何帮助将非常感激。

最佳答案

这将需要使用各种 aggregationsES 2.x 。首先,我使用 terms aggregation 获取所有唯一的 campaign_id 。然后我是 filteringactions 并使用该特定操作获取文档的 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 设置 action 映射,因为 Click 不会匹配 click。

希望这可以帮助!!

关于ElasticSearch - 计算聚合桶之间的比率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34839641/

10-10 23:43