|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// The OpenSearch Contributors require contributions made to |
| 4 | +// this file be licensed under the Apache-2.0 license or a |
| 5 | +// compatible open source license. |
| 6 | +// |
| 7 | +//go:build integration |
| 8 | + |
| 9 | +package osapi_test |
| 10 | + |
| 11 | +import ( |
| 12 | + "context" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/opensearch-project/opensearch-go/v4/osapi" |
| 19 | + osapitest "github.com/opensearch-project/opensearch-go/v4/osapi/internal/test" |
| 20 | + "github.com/opensearch-project/opensearch-go/v4/osapi/testutil" |
| 21 | +) |
| 22 | + |
| 23 | +func TestManual_Aggregation(t *testing.T) { |
| 24 | + client, err := testutil.NewClient(t) |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + index := testutil.MustUniqueString(t, "test-agg") |
| 28 | + t.Cleanup(func() { |
| 29 | + _, _ = client.Indices.Delete(context.Background(), &osapi.IndicesDeleteReq{Index: []string{index}}) |
| 30 | + }) |
| 31 | + |
| 32 | + _, err = client.Indices.Create(t.Context(), osapi.IndicesCreateReq{ |
| 33 | + Index: index, |
| 34 | + BodyReader: strings.NewReader( |
| 35 | + `{"mappings":{"properties":{"category":{"type":"keyword"},` + |
| 36 | + `"price":{"type":"integer"},"timestamp":{"type":"date"}}}}`, |
| 37 | + ), |
| 38 | + }) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + docs := []string{ |
| 42 | + `{"category":"electronics","price":100,"timestamp":"2024-01-01"}`, |
| 43 | + `{"category":"electronics","price":200,"timestamp":"2024-01-15"}`, |
| 44 | + `{"category":"books","price":15,"timestamp":"2024-02-01"}`, |
| 45 | + `{"category":"books","price":25,"timestamp":"2024-02-15"}`, |
| 46 | + `{"category":"clothing","price":50,"timestamp":"2024-03-01"}`, |
| 47 | + } |
| 48 | + |
| 49 | + var ndjson strings.Builder |
| 50 | + for _, doc := range docs { |
| 51 | + ndjson.WriteString(`{"index":{"_index":"` + index + `"}}` + "\n") |
| 52 | + ndjson.WriteString(doc + "\n") |
| 53 | + } |
| 54 | + _, err = client.Bulk(t.Context(), osapi.BulkReq{ |
| 55 | + Body: strings.NewReader(ndjson.String()), |
| 56 | + Params: &osapi.BulkParams{Refresh: "true"}, |
| 57 | + }) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + query string |
| 63 | + check func(t *testing.T, resp *osapi.SearchResp) |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "terms aggregation", |
| 67 | + query: `{"size":0,"aggs":{"by_category":{"terms":{"field":"category"}}}}`, |
| 68 | + check: func(t *testing.T, resp *osapi.SearchResp) { |
| 69 | + t.Helper() |
| 70 | + require.Contains(t, resp.Aggregations, "by_category") |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "date histogram aggregation", |
| 75 | + query: `{"size":0,"aggs":{"by_month":{"date_histogram":` + |
| 76 | + `{"field":"timestamp","calendar_interval":"month"}}}}`, |
| 77 | + check: func(t *testing.T, resp *osapi.SearchResp) { |
| 78 | + t.Helper() |
| 79 | + require.Contains(t, resp.Aggregations, "by_month") |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "stats aggregation", |
| 84 | + query: `{"size":0,"aggs":{"price_stats":{"stats":{"field":"price"}}}}`, |
| 85 | + check: func(t *testing.T, resp *osapi.SearchResp) { |
| 86 | + t.Helper() |
| 87 | + require.Contains(t, resp.Aggregations, "price_stats") |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "nested terms with stats", |
| 92 | + query: `{"size":0,"aggs":{"by_category":{"terms":{"field":"category"},` + |
| 93 | + `"aggs":{"avg_price":{"avg":{"field":"price"}}}}}}`, |
| 94 | + check: func(t *testing.T, resp *osapi.SearchResp) { |
| 95 | + t.Helper() |
| 96 | + require.Contains(t, resp.Aggregations, "by_category") |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + resp, err := client.Search(t.Context(), &osapi.SearchReq{ |
| 104 | + Index: []string{index}, |
| 105 | + BodyReader: strings.NewReader(tt.query), |
| 106 | + }) |
| 107 | + require.NoError(t, err) |
| 108 | + require.NotNil(t, resp.Aggregations) |
| 109 | + tt.check(t, resp) |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + t.Run("inspect", func(t *testing.T) { |
| 114 | + failingClient, err := osapitest.CreateFailingClient(t) |
| 115 | + require.NoError(t, err) |
| 116 | + |
| 117 | + res, err := failingClient.Search(t.Context(), &osapi.SearchReq{ |
| 118 | + Index: []string{index}, |
| 119 | + BodyReader: strings.NewReader(`{"size":0,"aggs":{"x":{"terms":{"field":"category"}}}}`), |
| 120 | + }) |
| 121 | + require.Error(t, err) |
| 122 | + require.NotNil(t, res) |
| 123 | + osapitest.VerifyInspect(t, res.Inspect()) |
| 124 | + }) |
| 125 | +} |
0 commit comments