Skip to content

Aggregations should be a generic in responses#1596

Merged
delvedor merged 7 commits intomainfrom
generics-aggregations
Dec 16, 2021
Merged

Aggregations should be a generic in responses#1596
delvedor merged 7 commits intomainfrom
generics-aggregations

Conversation

@delvedor
Copy link
Copy Markdown
Member

@delvedor delvedor commented Nov 3, 2021

As titled, this allows to write code like this:

import { Client } from '@elastic/elasticsearch'
import * as T from '@elastic/elasticsearch/lib/api/types'

const client = new Client({
  node: 'http://localhost:9200'
})

interface Document {
  world: number
}

interface Aggregation {
  hello: T.AggregationsValueAggregate
}

async function run () {
  const result = await client.search<Document, Aggregation>({
    index: 'foo',
    query: { match_all: {} },
    aggregations: {
      hello: {
        avg: {
          field: 'world'
        }
      }
    }
  })

  result.aggregations.hello // autocompletes to AggregationsValueAggregate
}

}

export interface RollupRollupSearchResponse<TDocument = unknown> {
export interface RollupRollupSearchResponse<TDocument = unknown, TAggregations = Record<AggregateName, AggregationsAggregate>> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we enforce TAggregations abide by Record<AggregateName, AggregationsAggregate> interface?
TAggregations extends Record<AggregateName, AggregationsAggregate> = Record<AggregateName, AggregationsAggregate>?
However,AggregationsAggregate is a union of types, I'm not sure it won't disrupt API consumers' code. We can do it in a follow-up.

Copy link
Copy Markdown
Contributor

@mshustov mshustov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall approach looks good to me. Curious to hear @dgieselaar since the APM team has to work around the lack of aggregations typed parameter in the SearchResponse interface

@dgieselaar
Copy link
Copy Markdown

Works for me! Small change on our side.

@delvedor delvedor marked this pull request as ready for review December 16, 2021 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants