|
| 1 | +// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-license. |
| 3 | +// See http://olivere.mit-license.org/license.txt for details. |
| 4 | + |
| 5 | +package elastic |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRuntimeMappingsSource(t *testing.T) { |
| 15 | + rm := RuntimeMappings{ |
| 16 | + "day_of_week": map[string]interface{}{ |
| 17 | + "type": "keyword", |
| 18 | + }, |
| 19 | + } |
| 20 | + src, err := rm.Source() |
| 21 | + if err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + data, err := json.Marshal(src) |
| 25 | + if err != nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | + expected := `{"day_of_week":{"type":"keyword"}}` |
| 29 | + if want, have := expected, string(data); want != have { |
| 30 | + t.Fatalf("want %s, have %s", want, have) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestRuntimeMappings(t *testing.T) { |
| 35 | + client := setupTestClient(t) //, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 36 | + |
| 37 | + ctx := context.Background() |
| 38 | + indexName := testIndexName |
| 39 | + |
| 40 | + // Create index |
| 41 | + createIndex, err := client.CreateIndex(indexName).Do(ctx) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + if createIndex == nil { |
| 46 | + t.Errorf("expected result to be != nil; got: %v", createIndex) |
| 47 | + } |
| 48 | + |
| 49 | + mapping := `{ |
| 50 | + "dynamic": "runtime", |
| 51 | + "properties": { |
| 52 | + "@timestamp": { |
| 53 | + "type":"date" |
| 54 | + } |
| 55 | + }, |
| 56 | + "runtime": { |
| 57 | + "day_of_week": { |
| 58 | + "type": "keyword", |
| 59 | + "script": { |
| 60 | + "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))" |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }` |
| 65 | + type Doc struct { |
| 66 | + Timestamp time.Time `json:"@timestamp"` |
| 67 | + } |
| 68 | + type DynamicDoc struct { |
| 69 | + Timestamp time.Time `json:"@timestamp"` |
| 70 | + DayOfWeek string `json:"day_of_week"` |
| 71 | + } |
| 72 | + |
| 73 | + // Create mapping |
| 74 | + putResp, err := client.PutMapping(). |
| 75 | + Index(indexName). |
| 76 | + BodyString(mapping). |
| 77 | + Do(ctx) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("expected put mapping to succeed; got: %v", err) |
| 80 | + } |
| 81 | + if putResp == nil { |
| 82 | + t.Fatalf("expected put mapping response; got: %v", putResp) |
| 83 | + } |
| 84 | + if !putResp.Acknowledged { |
| 85 | + t.Fatalf("expected put mapping ack; got: %v", putResp.Acknowledged) |
| 86 | + } |
| 87 | + |
| 88 | + // Add a document |
| 89 | + timestamp := time.Date(2021, 1, 17, 23, 24, 25, 26, time.UTC) |
| 90 | + indexResult, err := client.Index(). |
| 91 | + Index(indexName). |
| 92 | + Id("1"). |
| 93 | + BodyJson(&Doc{ |
| 94 | + Timestamp: timestamp, |
| 95 | + }). |
| 96 | + Refresh("wait_for"). |
| 97 | + Do(ctx) |
| 98 | + if err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + if indexResult == nil { |
| 102 | + t.Errorf("expected result to be != nil; got: %v", indexResult) |
| 103 | + } |
| 104 | + |
| 105 | + // Execute a search to check for runtime fields |
| 106 | + searchResp, err := client.Search(indexName). |
| 107 | + Query(NewMatchAllQuery()). |
| 108 | + DocvalueFields("@timestamp", "day_of_week"). |
| 109 | + Do(ctx) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + if searchResp == nil { |
| 114 | + t.Errorf("expected result to be != nil; got: %v", searchResp) |
| 115 | + } |
| 116 | + if want, have := int64(1), searchResp.TotalHits(); want != have { |
| 117 | + t.Fatalf("expected %d search hits, got %d", want, have) |
| 118 | + } |
| 119 | + |
| 120 | + // The hit should not have the "day_of_week" |
| 121 | + hit := searchResp.Hits.Hits[0] |
| 122 | + var doc DynamicDoc |
| 123 | + if err := json.Unmarshal(hit.Source, &doc); err != nil { |
| 124 | + t.Fatalf("unable to deserialize hit: %v", err) |
| 125 | + } |
| 126 | + if want, have := timestamp, doc.Timestamp; want != have { |
| 127 | + t.Fatalf("expected timestamp=%v, got %v", want, have) |
| 128 | + } |
| 129 | + if want, have := "", doc.DayOfWeek; want != have { |
| 130 | + t.Fatalf("expected day_of_week=%q, got %q", want, have) |
| 131 | + } |
| 132 | + |
| 133 | + // The fields should include a "day_of_week" of ["Sunday"] |
| 134 | + dayOfWeekIntfSlice, ok := hit.Fields["day_of_week"].([]interface{}) |
| 135 | + if !ok { |
| 136 | + t.Fatalf("expected a slice of strings, got %T", hit.Fields["day_of_week"]) |
| 137 | + } |
| 138 | + if want, have := 1, len(dayOfWeekIntfSlice); want != have { |
| 139 | + t.Fatalf("expected a slice of size %d, have %d", want, have) |
| 140 | + } |
| 141 | + dayOfWeek, ok := dayOfWeekIntfSlice[0].(string) |
| 142 | + if !ok { |
| 143 | + t.Fatalf("expected an element of string, got %T", dayOfWeekIntfSlice[0]) |
| 144 | + } |
| 145 | + if want, have := "Sunday", dayOfWeek; want != have { |
| 146 | + t.Fatalf("expected day_of_week=%q, have %q", want, have) |
| 147 | + } |
| 148 | +} |
0 commit comments