# Maximum ID from tweets of a specific user

**URL:** <https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419>\
**Category:** Elasticsearch\
**Created:** [July 25, 2017, 5:59am UTC](https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419 "2017-07-25T05:59:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cehpanku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cehpanku/32/8823_2.png) [@cehpanku](https://discuss.elastic.co/u/cehpanku)\
**Post date:** [July 25, 2017, 5:59am UTC](https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419/1 "2017-07-25T05:59:10Z")

</div>

Dear all,

I am storing the tweets from twitter into elasticsearch & I want to get the maximum value of a field {id} for a specific user ID {user.screen\_name}. Please help me with the elasticsearch query part. Right Now i am using this query.

POST /twitter/\_search?size=0  
{  
"aggs" : {  
"max\_id" : { "max" : { "field" : "id" } }  
}  
}  
Thanks in Advance.

Regards  
Pankaj

---

<div class="post-metadata">

**Author:** ![DPattee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dpattee/32/37772_2.png) [@DPattee](https://discuss.elastic.co/u/DPattee)\
**Post date:** [July 25, 2017, 6:17am UTC](https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419/2 "2017-07-25T06:17:16Z")

</div>

Do you mean:

1. You want the max id for a single specified name  
-- You need a query\_string or terms filter like

> ```
> "query": {
> "bool": {
> "must": [
> {
> "query_string": {
> "query": "user.screen_name: TARGETNAME",
> "analyze_wildcard": true
> }
> }
> 
> ```

1. You want the max id for every unique name  
-- You do a terms aggregation by name before you do the max aggregation by id

> ```
> "aggs": {
> "uniquenames": {
> "terms": {
> "field": "screen_name",
> "order": {
> "maxpost": "asc"
> }
> },
> "aggs": {
> "maxpost": {
> "max": {
> "field": "id"
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![cehpanku](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cehpanku/32/8823_2.png) [@cehpanku](https://discuss.elastic.co/u/cehpanku)\
**Post date:** [July 25, 2017, 7:52am UTC](https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419/3 "2017-07-25T07:52:29Z")

</div>

I am using this query. But this is not returning desired result. I want to query elasticsearch using a specific username from field {user.screen\_name} and return the maximum value of field {id}.

POST /twitter/\_search  
{  
"aggs": {  
"uniquenames": {  
"terms": {  
"field": "user.screen\_name.keyword",  
"order": {  
"maxpost": "desc"  
}  
},  
"aggs": {  
"maxpost": {  
"max": {  
"field": "id"  
}  
}  
}  
}  
}  
}

---

<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:** [August 22, 2017, 7:52am UTC](https://discuss.elastic.co/t/maximum-id-from-tweets-of-a-specific-user/94419/4 "2017-08-22T07:52:42Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
