ForestAdmin / forestadmin-experimental

3 stars 0 forks source link

datasource-elasticsearch - Aggregate Count groupBy does not work #9

Open Thenkei opened 1 year ago

Thenkei commented 1 year ago

The following snippets should create an aggregate boolean query

const renderingIds = await context.dataSource.getCollection('es-activity-logs').aggregate(
      {},
      {
        operation: 'Count',
        groups: [{ field: 'renderingId' }],
      },
      100_000,
    );

Expected resulting aggs query:

aggs: {
  renderingId: {
    terms: {
      field: 'renderingId',
      size: 100_000,
    },
  },
},

Recap

COUNT

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html

{
  operation: 'Count',
  field: 'renderingId',
},

Should create query

"aggs" : {
    "renderingIds_count" : { "value_count" : { "field" : "renderingId" } }
  }

Group by COUNT

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

{
  operation: 'Count',
  groups: [{ field: 'renderingId' }],
},

Should create query with limit = search.max_buckets

"aggs" : {
    "count-groupBy-renderingId" : { "terms" : { "field" : "renderingId"  } }
  }

Multiple groups - To confirm ???

{
  operation: 'Average',
  field: 'timing',
  groups: [{ field: 'country' }, { field: 'force' }],
},
 "aggs": {
        "group_by_country": {
            "terms": { "field": "country" },
            "terms": { "field": "force" },
            "aggs": {
                "average_balance": {
                    "avg": {
                        "field": "timing"
                    }
                }
            }
        }
    }
Thenkei commented 11 months ago

In order to resolve this and ease future development we are gonna:

TDD

Write an aggregation integration test suite. Goal: Reusable at some point by every DS.

Revamp

Rewrite the whole code.

Document

Document query interface limitation regarding ES Goal: Let users use the native driver (elastic search) for complex aggregation.. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html