Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/lib/aggregations/get-range.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { HomeDocument } from 'test/module'
import { Range } from 'lib/common'
import { HomeDocument } from 'test/module'
import { TEST_ELASTICSEARCH_NODE } from 'test/constants'
import { setupNestApplication } from 'test/toolkit'
import { ElasticsearchModule } from 'module/elasticsearch.module'
import { ElasticsearchService } from 'module/elasticsearch.service'
import { getRangeAggregation } from './get-range'

describe('getRangeAggregation', () => {
const { app } = setupNestApplication({
imports: [
ElasticsearchModule.register({
node: TEST_ELASTICSEARCH_NODE
})
]
})

const ranges: Array<Range> = [
{
from: 10
Expand All @@ -27,5 +39,39 @@ describe('getRangeAggregation', () => {
})
})

it('queries es for range aggregation', async () => {
const service = app.get(ElasticsearchService)
const result = await service.search(HomeDocument, {
size: 10,
aggregations: {
test: getRangeAggregation('propertyAreaSquared', ranges)
}
})

expect(result.aggregations.test).toStrictEqual({
buckets: [
{
// eslint-disable-next-line camelcase
doc_count: 0,
key: '*-25.0',
to: 25
},
{
// eslint-disable-next-line camelcase
doc_count: 19,
key: '10.0-*',
from: 10
},
{
// eslint-disable-next-line camelcase
doc_count: 0,
key: '15.0-20.0',
from: 15,
to: 20
}
]
})
})

test.todo('accepts only schema field with keyword')
})
9 changes: 9 additions & 0 deletions src/lib/common/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ export type Range = {
from?: number
to?: number
}

export type RangeBucket<TKey = string> = {
key: TKey
doc_count: number
} & Range

export type RangeBuckets = {
buckets: Array<RangeBucket>
}
7 changes: 5 additions & 2 deletions src/lib/transformers/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { estypes } from '@elastic/elasticsearch'
import { Bucket, Buckets, CompositeBuckets, Document, Hits, OptionalValue, Value } from 'lib/common'
import { Bucket, Buckets, CompositeBuckets, Document, Hits, OptionalValue, RangeBuckets, Value } from 'lib/common'
import {
Aggregations,
AggregationsBody,
Expand All @@ -11,6 +11,7 @@ import {
MaxAggregation,
MinAggregation,
PercentileAggregation,
RangeAggregation,
StatsBucketAggregation,
SumAggregation,
TermsAggregation,
Expand Down Expand Up @@ -42,7 +43,9 @@ export type TransformAggregation<
? estypes.StatsAggregate
: TAggregation extends PercentileAggregation<TDocument>
? estypes.TDigestPercentilesAggregate
: `Unhandled aggregation type for name: ${TName & string}`
: TAggregation extends RangeAggregation<TDocument>
? RangeBuckets
: `Unhandled aggregation type for name: ${TName & string}`

export type TransformedAggregation<
TDocument extends Document,
Expand Down