Skip to content

Aggregations API #21

Open
Open
@dvirsky

Description

@dvirsky

This is a proposed API for aggregations.

The aggregation pipeline is very suitable for a builder/fluent style API. It basically includes the following elements, repeating and transforming the pipeline:

  1. The base filter query (non repeating)
  2. Load - load properties from the document (if they are not in the sortables)
  3. Group by (with its reducers)
  4. Sort by
  5. Apply expression on values
  6. Limit

These are chained repeatably to transform the pipeline. So here's what I have in mind:

# query can be a string or a structured query object
req = AggregateRequest(query)
         .load('@foo', '@bar')
         .group_by(('@foo', '@bar'), 
                   #reducers 
                   count().as('total'),
                  count_distinct('@bar').as('num_bars'),
                  # alternative proposal
                  num_bars = count_distinct('@bar')
          )
          .apply("sqrt(@foo/@num_bars)", as='sqr')
          .sort_by(Sort.desc('@sqr'), Sort.asc('@other'), max_results = 100)
          .limit(0, 10)
          
resp = client.aggregate(req)  

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions