Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

Is it possible to fetch count of total number of docs that contain a qualifying aggregation condition in elasticsearch?

I use ES v7.3 and as per my requirements I am aggregating some fields to fetch the required docs in response, further their is a requirement to fetch the count of total number of all such docs also that contain the nested field which qualifies the aggregation condition as described below but I did not find a way where I am able to do that.

Current aggregation query that I am using to fetch the documents is,


"aggs": {
    "users": {
      "composite": {
        "sources": [
          {
            "users": {
              "terms": {
                "field": "co_profileId.keyword"
              }
            }
          }
        ],
        "size": 5000
      },
      "aggs": {
        "sessions": {
          "nested": {
            "path": "co_score"
          },
          "aggs": {
            "last_4_days": {
              "filter": {
                "range": {
                  "co_score.sessionTime": {
                    "gte": "2021-01-10T00:00:31.399Z",
                    "lte": "2021-01-14T01:37:31.399Z"
                  }
                }
              },
              "aggs": {
                "score_count": {
                  "sum": {
                    "field": "co_score.value"
                  }
                }
              }
            }
          }
        },
        "page_view_count_filter": {
          "bucket_selector": {
            "buckets_path": {
              "sessionCount": "sessions  > last_4_days > score_count"
            },
            "script": "params.sessionCount > 100"
          }
        },
        "filtered_users": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": [
                "co_profileId",
                "co_type",
                "co_score"
              ]
            }
          }
        }
      }
    }
  }

Sample doc:

{
    "co_profileId": "14654325",
    "co_type": "identify",
    "co_updatedAt": "2021-01-11T11:37:33.499Z",
    "co_score": [
        {
            "value": 3,
            "sessionTime": "2021-01-09T01:37:31.399Z"
        },
        {
            "value": 3,
            "sessionTime": "2021-01-10T10:47:33.419Z"
        },
        {
            "value": 6,
            "sessionTime": "2021-01-11T11:37:33.499Z"
        }
    ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...