Skip to main content

PyPI Latest Release License Python package Coverage Docs Code style: black

What is it?

pandagg is a Python package providing a simple interface to manipulate ElasticSearch queries and aggregations. Its goal is to make it the easiest possible to explore data indexed in an Elasticsearch cluster.

Some of its interactive features are inspired by pandas library, hence the name pandagg which aims to apply pandas to Elasticsearch aggregations.

pandagg is also greatly inspired by the official high level python client elasticsearch-dsl, and is intended to make it more convenient to deal with deeply nested queries and aggregations.

Features

  • flexible aggregation and search queries declaration, with ability to insert clauses at specific points (and not only below last manipulated clause)
  • query validation based on provided mappings
  • parsing of aggregation results in convenient formats: tree with interactive navigation, csv-like tabular breakdown, pandas dataframe, and others
  • cluster indices discovery module, and mappings interactive navigation

Documentation

Full documentation and user-guide are available here on read-the-docs.

Installation

pip install pandagg

Dependencies

Hard dependency: ligthtree

Soft dependency: to parse aggregation results as tabular dataframe: pandas

Quick demo

Discover indices on cluster with matching pattern:

>>> from elasticsearch import Elasticsearch
>>> from pandagg.discovery import discover
>>> client = Elasticsearch(hosts=['localhost:9200'])


>>> indices = discover(client, "mov*")
>>> indices
<Indices> ['movies', 'movies_fake']

Explore index mappings:

>>> movies = indices.movies
>>> movies.mappings
<Mappings>
_
├── directors                                                [Nested]
   ├── director_id                                           Keyword
   ├── first_name                                            Text
      └── raw                                             ~ Keyword
   ├── full_name                                             Text
      └── raw                                             ~ Keyword
   ├── genres                                                Keyword
   └── last_name                                             Text
       └── raw                                             ~ Keyword
├── genres                                                    Keyword
├── movie_id                                                  Keyword
├── name                                                      Text
...
>>> movies.mappings.roles
<Mappings subpart: roles>
roles                                                        [Nested]
├── actor_id                                                  Keyword
├── first_name                                                Text
   └── raw                                                 ~ Keyword
├── full_name                                                 Text
   └── raw                                                 ~ Keyword
├── gender                                                    Keyword
├── last_name                                                 Text
   └── raw                                                 ~ Keyword
└── role                                                      Keyword

Execute aggregation on field:

>>> movies.mappings.roles.gender.a.terms()
   doc_count key
M    2296792   M
F    1135174   F

Build search request:

>> > search = movies
    .search()
    .size(2)
    .groupby('decade', 'histogram', interval=10, field='year')
    .groupby('genres', size=3)
    .agg('avg_rank', 'avg', field='rank')
    .agg('avg_nb_roles', 'avg', field='nb_roles')
    .filter('range', year={"gte": 1990})

>> > search.to_dict()
{'aggs': {'decade': {u'aggs': {'genres': {u'aggs': {'avg_nb_roles': {u'avg': {'field': 'nb_roles'}},
                                                    'avg_rank': {u'avg': {'field': 'rank'}}},
                                          'terms': {'field': 'genres', 'size': 3}}},
                     'histogram': {'field': 'year', 'interval': 10}}},
 'query': {'bool': {u'filter': [{'range': {'year': {'gte': 1990}}}]}},
 'size': 2}

Execute it:

>>> response = search.execute()
>>> response
<Response> took 52ms, success: True, total result >=10000, contains 2 hits

Parse it in tabular format:

>>> response.aggregations.to_dataframe()
                    avg_nb_roles  avg_rank  doc_count
decade genres
2000.0 Drama           14.385391  6.269675      11500
1990.0 Documentary      3.778982  6.517093       8393
2000.0 Short            4.053082  6.836253      13451
       Documentary      5.581433  6.980898       8639
1990.0 Short            3.023284  6.311326      12197
       Drama           18.518067  5.981429      12232

Disclaimers

It does not ensure retro-compatible with previous versions of elasticsearch (intended to work with >=7). It is part of the roadmap to tag pandagg versions according to the ElasticSearch versions they are related to (ie v7.1.4 would work with Elasticsearch v7.X.X).

It doesn't provide yet all functionalities provided by the official client (for instance ORM like insert/updates, index operations etc..). Primary focus of pandagg was on read operations.

Contributing

All contributions, bug reports, bug fixes, documentation improvements, enhancements and ideas are welcome.

Roadmap

  • on aggregation nodes, ensure all allowed fields are listed
  • expand functionalities: proper ORM similar to elasticsearch-dsl Document classes, index managing operations
  • package versions for different ElasticSearch versions
  • composite aggregation iterator
  • clean and proper documentation

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandagg-0.2.1.tar.gz (77.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pandagg-0.2.1-py3-none-any.whl (99.3 kB view details)

Uploaded Python 3

File details

Details for the file pandagg-0.2.1.tar.gz.

File metadata

  • Download URL: pandagg-0.2.1.tar.gz
  • Upload date:
  • Size: 77.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for pandagg-0.2.1.tar.gz
Algorithm Hash digest
SHA256 55c0eb53de88390456f41026bff6c2fdf1783ab314548873a493b8bd691713e8
MD5 36b53982f8c7e3c20bb00b874552cc6f
BLAKE2b-256 67dc3c9d5eb24136cb87717502abc80aa41a12a2247413a11732a562b1a34993

See more details on using hashes here.

File details

Details for the file pandagg-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pandagg-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 99.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for pandagg-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d8a69b28f38f55857667982e7e60c076edd078de7d8679f525ed010e23c2c98e
MD5 b6e860aa8ad0dcc7acd14b9a1daf0c5e
BLAKE2b-256 21e2dd9c6108a692d8611387633081703a456a424c66ca845b3e4fd3c7979471

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page