Creating a "Brand Lovers" segment in ElasticSearch

Viewed 14

I've got an ElasticSearch index that records hits, with a time and token field, unique to each user. I'd like to know, for the last x days, how many unique tokens I have that have had 15 or more hits. ("Brand Lovers" in marketing-speak.)

Any ideas on how to achieve this?

Thanks!

1 Answers

You can use the range query with terms aggregation to get required results

{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "hits": {
                            "gte": 15
                        }
                    }
                },
                {
                    "range": {
                        "time": {
                            "gte": "now-1d/d",
                            "lt": "now"
                        }
                    }
                }
            ],
            "minimum_should_match": 1
        }
    },
    "aggs": {
        "distinct_tokens": {
            "terms": {
                "field": "tokens"
            }
        }
    }
}
Related