Skip to content

calculate ratio between aggregation buckets #16040

@orrchen

Description

@orrchen

Hi,
I'm using the following terms aggregations to get views and clicks of each campaign ( by campaign_id ) :

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

This is the response I get:

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

Example of a document:

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

I need to be able to retrieve the conversion rate of each campaign ( clicks / views ) , and I can't do it on the client side since I need to be able to sort by conversion rate. Is it possible on elasticsearch?

Any help would be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions