# Getting document ids in facets query

**URL:** <https://discuss.elastic.co/t/getting-document-ids-in-facets-query/10238>\
**Category:** Elasticsearch\
**Created:** [January 6, 2013, 12:48pm UTC](https://discuss.elastic.co/t/getting-document-ids-in-facets-query/10238 "2013-01-06T12:48:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [January 6, 2013, 12:48pm UTC](https://discuss.elastic.co/t/getting-document-ids-in-facets-query/10238/1 "2013-01-06T12:48:40Z")

</div>

Hi  
Is it possible to return also document ids inside a facet query ? Example :

{  
"from": 0,  
"size": 0,  
"explain": false,  
"min\_score": 0.01,  
"facets": {

```
   "ownsOrdered": {
        "terms": {
            "field": "owns",
             "size" : 1,
            "order": "reverse_term"
        }
    }
},
    
"query": {
   // bla bla query
},
"fields": [
    "id"
   
]

```

}

The result of that query is :

{  
"took": 0,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 11894,  
"max\_score": 1,  
"hits": []  
},  
"facets": {  
"ownsOrdered": {  
"\_type": "terms",  
"missing": 0,  
"total": 11894,  
"other": 11892,  
"terms": [  
{  
"term": 4,  
"count": 2  
}  
]  
}  
}  
}

Is there any way to attach to the "ownsOrdered" facet the document ids  
matching the facet ?  
Of course i could do another query that orders the documents upon the  
relevant facet , but i just dont want to make 2 queries.

--

---

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [January 6, 2013, 2:35pm UTC](https://discuss.elastic.co/t/getting-document-ids-in-facets-query/10238/2 "2013-01-06T14:35:00Z")

</div>

found it : terms\_stats does exactly that

On Sunday, January 6, 2013 2:48:40 PM UTC+2, Roman Kournjaev wrote:

> Hi  
> Is it possible to return also document ids inside a facet query ? Example  
> :
> 
> {  
> "from": 0,  
> "size": 0,  
> "explain": false,  
> "min\_score": 0.01,  
> "facets": {
> 
> ```
> "ownsOrdered": {
> "terms": {
> "field": "owns",
> "size" : 1,
> "order": "reverse_term"
> }
> }
> },
>     
> "query": {
> // bla bla query
> },
> "fields": [
> "id"
>    
> ]
> 
> ```
> 
> }
> 
> The result of that query is :
> 
> {  
> "took": 0,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 11894,  
> "max\_score": 1,  
> "hits":   
> },  
> "facets": {  
> "ownsOrdered": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 11894,  
> "other": 11892,  
> "terms": [  
> {  
> "term": 4,  
> "count": 2  
> }  
> ]  
> }  
> }  
> }
> 
> Is there any way to attach to the "ownsOrdered" facet the document ids  
> matching the facet ?  
> Of course i could do another query that orders the documents upon the  
> relevant facet , but i just dont want to make 2 queries.

--

---

<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, 2:57am UTC](https://discuss.elastic.co/t/getting-document-ids-in-facets-query/10238/3 "2017-07-06T02:57:42Z")

</div>


