From a92c4923142bb3b53e41a10a374ef46d8d0efdba Mon Sep 17 00:00:00 2001 From: kkewwei Date: Mon, 30 Sep 2024 00:37:40 +0800 Subject: [PATCH] Added aggregation tests for avg, max, min, range, terms, sum, nested and histogram. (#576) * Add aggregation tests for nested Signed-off-by: kkewwei Signed-off-by: kewei.11 * Added aggregation tests for avg, max, min, range, terms, sum and histogram. Signed-off-by: dblock Signed-off-by: kkewwei --------- Signed-off-by: kkewwei Signed-off-by: kewei.11 Signed-off-by: dblock Co-authored-by: dblock --- CHANGELOG.md | 1 + spec/schemas/_common.aggregations.yaml | 5 +- .../_core/search/aggregations/avg.yaml | 38 ++++++++++++ .../_core/search/aggregations/historgram.yaml | 48 +++++++++++++++ .../_core/search/aggregations/max.yaml | 38 ++++++++++++ .../_core/search/aggregations/min.yaml | 38 ++++++++++++ .../_core/search/aggregations/nested.yaml | 59 +++++++++++++++++++ .../_core/search/aggregations/range.yaml | 46 +++++++++++++++ .../_core/search/aggregations/sum.yaml | 38 ++++++++++++ .../_core/search/aggregations/terms.yaml | 54 +++++++++++++++++ 10 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 tests/default/_core/search/aggregations/avg.yaml create mode 100644 tests/default/_core/search/aggregations/historgram.yaml create mode 100644 tests/default/_core/search/aggregations/max.yaml create mode 100644 tests/default/_core/search/aggregations/min.yaml create mode 100644 tests/default/_core/search/aggregations/nested.yaml create mode 100644 tests/default/_core/search/aggregations/range.yaml create mode 100644 tests/default/_core/search/aggregations/sum.yaml create mode 100644 tests/default/_core/search/aggregations/terms.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f7833649..1081c7af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixed `/_plugins/_transform/_preview` ([#568](https://github.com/opensearch-project/opensearch-api-specification/pull/568)) - Fixed create/delete/index operation in `_bulk` ([#582](https://github.com/opensearch-project/opensearch-api-specification/pull/582)) - Add `mode` and `compression` to k-NN index creation and search, and add `rescore` and `oversample_factor` to k-NN search ([#588](https://github.com/opensearch-project/opensearch-api-specification/pull/588)) +- Fixed `/{index}/_search` with aggregations ([#576](https://github.com/opensearch-project/opensearch-api-specification/pull/576)) ### Security diff --git a/spec/schemas/_common.aggregations.yaml b/spec/schemas/_common.aggregations.yaml index 3313b096b..be7ef28a4 100644 --- a/spec/schemas/_common.aggregations.yaml +++ b/spec/schemas/_common.aggregations.yaml @@ -7,7 +7,7 @@ paths: {} components: schemas: Aggregate: - oneOf: + anyOf: - $ref: '#/components/schemas/CardinalityAggregate' - $ref: '#/components/schemas/HdrPercentilesAggregate' - $ref: '#/components/schemas/HdrPercentileRanksAggregate' @@ -786,6 +786,9 @@ components: allOf: - $ref: '#/components/schemas/SingleBucketAggregateBase' - type: object + properties: + doc_count: + type: number ReverseNestedAggregate: allOf: - $ref: '#/components/schemas/SingleBucketAggregateBase' diff --git a/tests/default/_core/search/aggregations/avg.yaml b/tests/default/_core/search/aggregations/avg.yaml new file mode 100644 index 000000000..52daa7ecb --- /dev/null +++ b/tests/default/_core/search/aggregations/avg.yaml @@ -0,0 +1,38 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test avg aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, duration: 126} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, duration: 100} +chapters: + - synopsis: Aggregate with avg. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + duration_avg: + avg: + field: duration + response: + status: 200 + payload: + aggregations: + duration_avg: + value: 113 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/historgram.yaml b/tests/default/_core/search/aggregations/historgram.yaml new file mode 100644 index 000000000..03adc94f4 --- /dev/null +++ b/tests/default/_core/search/aggregations/historgram.yaml @@ -0,0 +1,48 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test histogram aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, year: 2011} + - {create: {_index: movies}} + - {director: Bennett Miller, title: The Cruise, year: 1998} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, year: 1960} +chapters: + - synopsis: Aggregate with histogram. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + movies: + histogram: + field: year + interval: 100 + format: '##0.0' + response: + status: 200 + payload: + aggregations: + movies: + buckets: + - key_as_string: '1900.0' + key: 1900 + doc_count: 2 + - key_as_string: '2000.0' + key: 2000 + doc_count: 1 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/max.yaml b/tests/default/_core/search/aggregations/max.yaml new file mode 100644 index 000000000..4d05066b2 --- /dev/null +++ b/tests/default/_core/search/aggregations/max.yaml @@ -0,0 +1,38 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test max aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, duration: 126} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, duration: 100} +chapters: + - synopsis: Aggregate with max. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + duration_max: + max: + field: duration + response: + status: 200 + payload: + aggregations: + duration_max: + value: 126 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/min.yaml b/tests/default/_core/search/aggregations/min.yaml new file mode 100644 index 000000000..0d0f6197c --- /dev/null +++ b/tests/default/_core/search/aggregations/min.yaml @@ -0,0 +1,38 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test min aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, duration: 126} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, duration: 100} +chapters: + - synopsis: Aggregate with min. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + duration_min: + min: + field: duration + response: + status: 200 + payload: + aggregations: + duration_min: + value: 100 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/nested.yaml b/tests/default/_core/search/aggregations/nested.yaml new file mode 100644 index 000000000..4b519dcc5 --- /dev/null +++ b/tests/default/_core/search/aggregations/nested.yaml @@ -0,0 +1,59 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test aggregation. +prologues: + - path: books + method: PUT + request: + payload: + mappings: + properties: + details: + type: nested + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {index: {_index: books, _id: '0'}} + - {details: {title: title1}} + - {index: {_index: books, _id: '1'}} + - {details: {title: title2}} +chapters: + - synopsis: Aggregation with nested. + path: /{index}/_search + parameters: + index: books + method: POST + request: + payload: + size: 0 + aggregations: + details_agg: + nested: + path: details + response: + status: 200 + payload: + timed_out: false + _shards: + total: 1 + successful: 1 + skipped: 0 + failed: 0 + hits: + total: + value: 2 + relation: eq + max_score: null + hits: [] + aggregations: + details_agg: + doc_count: 2 + +epilogues: + - path: /books + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/range.yaml b/tests/default/_core/search/aggregations/range.yaml new file mode 100644 index 000000000..0c34de8db --- /dev/null +++ b/tests/default/_core/search/aggregations/range.yaml @@ -0,0 +1,46 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test range aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, year: 2011} + - {create: {_index: movies}} + - {director: Bennett Miller, title: The Cruise, year: 1998} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, year: 1960} +chapters: + - synopsis: Aggregate with range. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + 20th_century: + range: + field: year + ranges: + - from: 1900 + to: 2000 + response: + status: 200 + payload: + aggregations: + 20th_century: + buckets: + - from: 1900 + to: 2000 + doc_count: 2 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/sum.yaml b/tests/default/_core/search/aggregations/sum.yaml new file mode 100644 index 000000000..252785ca3 --- /dev/null +++ b/tests/default/_core/search/aggregations/sum.yaml @@ -0,0 +1,38 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test sum aggregation. +prologues: + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball, duration: 126} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive, duration: 100} +chapters: + - synopsis: Aggregate with sum. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + duration_sum: + sum: + field: duration + response: + status: 200 + payload: + aggregations: + duration_sum: + value: 226 +epilogues: + - path: /movies + method: DELETE + status: [200, 404] diff --git a/tests/default/_core/search/aggregations/terms.yaml b/tests/default/_core/search/aggregations/terms.yaml new file mode 100644 index 000000000..19c4a9606 --- /dev/null +++ b/tests/default/_core/search/aggregations/terms.yaml @@ -0,0 +1,54 @@ +$schema: ../../../../../json_schemas/test_story.schema.yaml + +description: Test terms aggregation. +prologues: + - path: /movies + method: PUT + request: + payload: + mappings: + properties: + director: + type: text + fielddata: true + fields: + raw: + type: keyword + - path: /_bulk + method: POST + parameters: + refresh: true + request: + content_type: application/x-ndjson + payload: + - {create: {_index: movies}} + - {director: Bennett Miller, title: Moneyball} + - {create: {_index: movies}} + - {director: Bennett Miller, title: The Cruise} + - {create: {_index: movies}} + - {director: Nicolas Winding Refn, title: Drive} +chapters: + - synopsis: Aggregate with terms. + path: /{index}/_search + parameters: + index: movies + method: POST + request: + payload: + size: 0 + aggregations: + directors: + terms: + field: director.raw + response: + status: 200 + payload: + aggregations: + directors: + buckets: + - key: Bennett Miller + doc_count: 2 +epilogues: + - path: /movies + method: DELETE + status: [200, 404]