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
131 changes: 131 additions & 0 deletions src/lib/aggregations/get-filter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { TEST_ELASTICSEARCH_NODE } from 'test/constants'
import { setupNestApplication } from 'test/toolkit'
import { HomeDocument, PropertyType } from 'test/module'
import { getBoolQuery, getMustQuery, getRangeQuery, getTermQuery } from 'lib/queries'
import { ElasticsearchService } from 'module/elasticsearch.service'
import { ElasticsearchModule } from 'module/elasticsearch.module'
import { getFilterAggregation } from './get-filter'
import { getSumAggregation } from './get-sum'

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

it('should accept term query', () => {
const query = getFilterAggregation<HomeDocument>(getTermQuery('address.keyword', 'test'))

expect(query).toEqual({
filter: {
term: {
'address.keyword': {
value: 'test'
}
}
}
})
})

it('should accept range query', () => {
const query = getFilterAggregation<HomeDocument>(
getRangeQuery('builtInYear', {
gt: 1000
})
)
expect(query).toEqual({
filter: {
range: {
builtInYear: {
gt: 1000
}
}
}
})
})

it('should accept bool query', () => {
const query = getFilterAggregation<HomeDocument>(getBoolQuery(getMustQuery(getTermQuery('propertyType.keyword', PropertyType.Flat))))

expect(query).toEqual({
filter: {
bool: {
must: {
term: {
'propertyType.keyword': {
value: 'Flat'
}
}
}
}
}
})
})

it('queries elasticsearch with term query', async () => {
const service = app.get(ElasticsearchService)

const result = await service.search(HomeDocument, {
size: 0,
aggregations: {
filterAggregation: getFilterAggregation(getTermQuery('propertyType.keyword', PropertyType.Flat))
}
})

expect(result.aggregations.filterAggregation.doc_count).not.toEqual(0)
})

it('queries elasticsearch with range query', async () => {
const service = app.get(ElasticsearchService)

const result = await service.search(HomeDocument, {
size: 0,
aggregations: {
filterAggregation: getFilterAggregation(
getRangeQuery('propertyAreaSquared', {
gt: 20000
})
)
}
})

expect(result.aggregations.filterAggregation.doc_count).not.toEqual(0)
})

it('queries elasticsearch with bool query', async () => {
const service = app.get(ElasticsearchService)

const result = await service.search(HomeDocument, {
size: 0,
aggregations: {
filterAggregation: getFilterAggregation(
getBoolQuery(getMustQuery([getTermQuery('hasProperty', true), getTermQuery('propertyType.keyword', PropertyType.Flat)]))
)
}
})

expect(result.aggregations.filterAggregation.doc_count).not.toEqual(0)
})

it('queries elasticsearch with term query and nested sum aggregation', async () => {
const service = app.get(ElasticsearchService)

const result = await service.search(HomeDocument, {
size: 0,
aggregations: {
filterAggregation: {
...getFilterAggregation(getTermQuery('propertyType.keyword', PropertyType.Flat)),
aggregations: {
sumAggregation: getSumAggregation('propertyAreaSquared')
}
}
}
})

expect(result.aggregations.filterAggregation.doc_count).not.toEqual(0)
expect(result.aggregations.filterAggregation.sumAggregation.value).not.toEqual(0)
})
})
12 changes: 12 additions & 0 deletions src/lib/aggregations/get-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Document } from 'lib/common'
import { BoolQuery, RangeQuery, TermQuery } from 'lib/queries'

export type QueryType<TDocument extends Document> = TermQuery<TDocument> | RangeQuery<TDocument> | BoolQuery<TDocument>

export type FilterAggregation<TDocument extends Document> = {
filter: QueryType<TDocument>
}

export const getFilterAggregation = <TDocument extends Document>(filter: QueryType<TDocument>): FilterAggregation<TDocument> => ({
filter
})
1 change: 1 addition & 0 deletions src/lib/aggregations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './get-top-hits'
export * from './get-composite'
export * from './get-stats-bucket'
export * from './get-nested'
export * from './get-filter'
2 changes: 2 additions & 0 deletions src/lib/aggregations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CardinalityAggregation } from './get-cardinality'
import { MinAggregation } from './get-min'
import { StatsBucketAggregation } from './get-stats-bucket'
import { NestedAggregation } from './get-nested'
import { FilterAggregation } from './get-filter'

export type AggregationList<TDocument extends Document> =
| AvgAggregation<TDocument>
Expand All @@ -34,6 +35,7 @@ export type AggregationList<TDocument extends Document> =
| CompositeAggregation<TDocument>
| StatsBucketAggregation
| NestedAggregation<TDocument>
| FilterAggregation<TDocument>

export type AggregationsContainer<TDocument extends Document> = Record<string, Aggregations<TDocument>>

Expand Down
3 changes: 3 additions & 0 deletions src/lib/responses/aggregation-bucket-base.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type AggregationBucketBaseResponse = {
doc_count: number
}
3 changes: 1 addition & 2 deletions src/lib/responses/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './search'
export * from './cluster-health'
export * from './missing-value-aggregation'
export * from './nested-aggregation'
export * from './aggregation-bucket-base.response'
3 changes: 0 additions & 3 deletions src/lib/responses/missing-value-aggregation.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/lib/responses/nested-aggregation.ts

This file was deleted.

11 changes: 7 additions & 4 deletions src/lib/transformers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CardinalityAggregation,
CompositeAggregation,
DateHistogramAggregation,
FilterAggregation,
HistogramAggregation,
MaxAggregation,
MinAggregation,
Expand All @@ -21,7 +22,7 @@ import {
TopHitsAggregation,
ValueCountAggregation
} from 'lib/aggregations'
import { MissingValueAggregationResponse, NestedAggregationResponse } from 'lib/responses'
import { AggregationBucketBaseResponse } from 'lib/responses'

export type TransformAggregation<
TDocument extends Document,
Expand All @@ -48,12 +49,14 @@ export type TransformAggregation<
: TAggregation extends RangeAggregation<TDocument>
? Buckets<string, RangeBucket & TransformedAggregations<TDocument, TAggregationsBody>>
: TAggregation extends MissingValueAggregation<TDocument>
? MissingValueAggregationResponse & TransformedAggregations<TDocument, TAggregationsBody>
? AggregationBucketBaseResponse & TransformedAggregations<TDocument, TAggregationsBody>
: TAggregation extends StatsBucketAggregation
? AggregationsStatsAggregate
: TAggregation extends NestedAggregation<TDocument>
? NestedAggregationResponse & TransformedAggregations<TDocument, TAggregationsBody>
: `Unhandled aggregation type for name: ${TName & string}`
? AggregationBucketBaseResponse & TransformedAggregations<TDocument, TAggregationsBody>
: TAggregation extends FilterAggregation<TDocument>
? AggregationBucketBaseResponse & TransformedAggregations<TDocument, TAggregationsBody>
: `Unhandled aggregation type for name: ${TName & string}`

export type TransformedAggregation<
TDocument extends Document,
Expand Down