# Translating ElasticSearch Facets Query into PyES

**URL:** <https://discuss.elastic.co/t/translating-elasticsearch-facets-query-into-pyes/8941>\
**Category:** Elasticsearch\
**Created:** [September 6, 2012, 6:15am UTC](https://discuss.elastic.co/t/translating-elasticsearch-facets-query-into-pyes/8941 "2012-09-06T06:15:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://discuss.elastic.co/u/Suraj)\
**Post date:** [September 6, 2012, 6:15am UTC](https://discuss.elastic.co/t/translating-elasticsearch-facets-query-into-pyes/8941/1 "2012-09-06T06:15:39Z")

</div>

I have a following query and I want to change that query into PyES:

```
{
  "facets": {
    "participating-org.name": {
      "terms": {
        "field": "participating-org.name"
      },
      "nested": "participating-org"
    }
  }
}

```

I have searched in PyES documentation about:

- class pyes.facets.TermsFacetFilter(field=None, values=None, \_name=None,  
execution=None, \*_kwargs)_

And I don't know how to use it plus I couldn't find any examples related to  
it. Hoping to see PyES guys coming out with good documentation with  
examples in future.

--

---

<div class="post-metadata">

**Author:** ![Suraj](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Suraj](https://discuss.elastic.co/u/Suraj)\
**Post date:** [September 7, 2012, 7:32am UTC](https://discuss.elastic.co/t/translating-elasticsearch-facets-query-into-pyes/8941/2 "2012-09-07T07:32:53Z")

</div>

I have just found out myself:

```
from pyes import *
from pyes.facets import *

conn = ES('localhost:9200', default_indices='org', default_types='activity')

q2 = MatchAllQuery().search()
q2.facet.add_term_facet('participating-org.role', nested="participating-org")

# Displays the ES JSON query.
print q2

resultset = conn.search(q2)

# To display the all resultsets.
for r in resultset:
    print r

# To display the facet counts.
print resultset.facets

```

This code gives the above JSON Code and gives the exact count for me.

On Thursday, September 6, 2012 12:00:39 PM UTC+5:45, Suraj Tamang wrote:

> I have a following query and I want to change that query into PyES:
> 
> ```
> {
> "facets": {
> "participating-org.name": {
> "terms": {
> "field": "participating-org.name"
> },
> "nested": "participating-org"
> }
> }
> }
> 
> ```
> 
> I have searched in PyES documentation about:
> 
> - class pyes.facets.TermsFacetFilter(field=None, values=None, \_name=None,  
> execution=None, \*_kwargs)_
> 
> And I don't know how to use it plus I couldn't find any examples related  
> to it. Hoping to see PyES guys coming out with good documentation with  
> examples in future.

--

---

<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, 3:13am UTC](https://discuss.elastic.co/t/translating-elasticsearch-facets-query-into-pyes/8941/3 "2017-07-06T03:13:50Z")

</div>


