平均百分比单据_计数

64jmpszr  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(168)

所以我知道我的总点击量是182个文档

"hits": {
        "total": {
            "value": 182,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },

然后我进行聚合,以了解有多少文档具有源instagagram或Twitter,并返回给我:

"bySource": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "instagram",
                    "doc_count": 162
                },
                {
                    "key": "twitter",
                    "doc_count": 20
                }
            ]
        }

有没有可能得到来自Twitter和Instagram的文档的百分比?因此,有89%的文档来自Instagram,11%的文档来自Twitter。

我的聚合代码如下所示:

"aggs": {
        "bySource": {
            "terms": {
                "field": "profile.source.keyword"
            }
        }
}

如果可能的话,请让我知道。谢谢

s1ag04yj

s1ag04yj1#

当然,使用‘Bucket脚本聚合’也是可能的。

示例查询可能如下所示:

{
    "size": 0,
    "aggs": {
        "filters_agg": {
            "filters": {
                "filters": {
                    "sourceCount": {
                        "match_all": {}
                    }
                }
            },
            "aggs": {
                "bySource": {
                    "terms": {
                        "field": "profile.source.keyword"
                    }
                },
                "instagram_count_percentage": {
                    "bucket_script": {
                        "buckets_path": {
                            "instagram_count": "bySource['instagram']>_count",
                            "total_count": "_count"
                        },
                        "script": "Math.round((params.instagram_count * 100)/params.total_count)"
                    }
                },
                "twitter_count_percentage": {
                    "bucket_script": {
                        "buckets_path": {
                            "twitter_count": "bySource['twitter']>_count",
                            "total_count": "_count"
                        },
                        "script": "Math.round((params.twitter_count * 100)/params.total_count)"
                    }
                }
            }
        }
    }
}

回应可能是这样的:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 182,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "filters_agg": {
      "buckets": {
        "sourceCount": {
          "doc_count": 182,
          "bySource": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "instagram",
                "doc_count": 162
              },
              {
                "key": "twitter",
                "doc_count": 20
              }
            ]
          },
          "instagram_count_percentage": {
            "value": 89
          },
          "twitter_count_percentage": {
            "value": 11
          }
        }
      }
    }
  }
}

根据你的情况和你的Map,试着调整它或获得灵感。

相关问题