Translating ElasticSearch Facets Query into PyES


(Suraj) #1

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.

--


(Suraj) #2

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.

--


(system) #3