# How to aggregate this kind of data

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-this-kind-of-data/18381>\
**Category:** Elasticsearch\
**Created:** [June 28, 2014, 3:02am UTC](https://discuss.elastic.co/t/how-to-aggregate-this-kind-of-data/18381 "2014-06-28T03:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helennie\_Nie](https://avatars.discourse-cdn.com/v4/letter/h/58f4c7/32.png) [@Helennie\_Nie](https://discuss.elastic.co/u/Helennie_Nie)\
**Post date:** [June 28, 2014, 3:02am UTC](https://discuss.elastic.co/t/how-to-aggregate-this-kind-of-data/18381/1 "2014-06-28T03:02:51Z")

</div>

Hi there

I have some data like :  
{  
"url":"xxx",  
"pv":n,  
"date":Date  
}  
url is a string and pv is an integer, what I want is to sum pv of an url  
form one date to another date, what aggregation should I use?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/630890a7-500f-4f86-b136-001c0a484353%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/630890a7-500f-4f86-b136-001c0a484353%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [June 30, 2014, 7:54am UTC](https://discuss.elastic.co/t/how-to-aggregate-this-kind-of-data/18381/2 "2014-06-30T07:54:11Z")

</div>

Hi Helennie,

Depending on your use case you can do any one of a few options:

1. Search for a specific date range, a specific URL and retrieve the count  
of pv for the URL over the date range:  
{  
"query": {  
"constant\_score": {  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"date": {  
"from": "2014/01/01",  
"to": "2014/01/04"  
}  
}  
},  
{  
"term": {  
"url": "[http://www.example.com](http://www.example.com)"  
}  
}  
]  
}  
}  
}  
},  
"aggs": {  
"pvCount": {  
"sum": {  
"field" : "pv"  
}  
}  
}  
}

2. Search for a specific date range and return an aggregation for the count  
of pv for the top N URLs ordered by pv:  
{  
"query": {  
"constant\_score": {  
"filter": {  
"range": {  
"date": {  
"from": "2014/01/01",  
"to": "2014/01/01"  
}  
}  
}  
}  
},  
"aggs": {  
"topURLs": {  
"terms": {  
"field": "url",  
"size": 10,  
"order": {  
"pvCount": "desc"  
}  
},  
"aggs": {  
"pvCount": {  
"sum": {  
"field" : "pv"  
}  
}  
}  
}  
}  
}

3. Return an aggregation which buckets date ranges and within those buckets  
returns the count of pv for the top N URLs ordered by pv:  
{  
"aggs": {  
"dateByDay": {  
"date\_histogram": {  
"field": "date",  
"interval": "day"  
},  
"aggs": {  
"topURLs": {  
"terms": {  
"field": "url",  
"size": 10,  
"order": {  
"pvCount": "desc"  
}  
},  
"aggs": {  
"pvCount": {  
"sum": {  
"field" : "pv"  
}  
}  
}  
}  
}  
}  
}  
}

I hope this helps clarify some of your options,

Regards,

Colin

On Saturday, 28 June 2014 05:02:51 UTC+2, Helennie Nie wrote:

> Hi there
> 
> I have some data like :  
> {  
> "url":"xxx",  
> "pv":n,  
> "date":Date  
> }  
> url is a string and pv is an integer, what I want is to sum pv of an url  
> form one date to another date, what aggregation should I use?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0b3c312c-00ad-41ad-ac89-852cc824a407%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0b3c312c-00ad-41ad-ac89-852cc824a407%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:18am UTC](https://discuss.elastic.co/t/how-to-aggregate-this-kind-of-data/18381/3 "2017-07-06T01:18:59Z")

</div>

Closed on July 6, 2017, 1:18am UTC


