Skip to content

Commit caca809

Browse files
committed
Add test for date math in index names
This should guard against #1009.
1 parent 5105d2e commit caca809

File tree

2 files changed

+86
-20
lines changed

2 files changed

+86
-20
lines changed

search_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package elastic
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
1011
"reflect"
1112
"testing"
1213
"time"
@@ -1392,3 +1393,68 @@ func TestSearchWithDocvalueFields(t *testing.T) {
13921393
}
13931394
}
13941395
}
1396+
1397+
func TestSearchWithDateMathIndices(t *testing.T) {
1398+
client := setupTestClient(t) //, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
1399+
1400+
ctx := context.Background()
1401+
now := time.Now().UTC()
1402+
indexNameToday := fmt.Sprintf("elastic-trail-%s", now.Format("2006.01.02"))
1403+
indexNameYesterday := fmt.Sprintf("elastic-trail-%s", now.AddDate(0, 0, -1).Format("2006.01.02"))
1404+
indexNameTomorrow := fmt.Sprintf("elastic-trail-%s", now.AddDate(0, 0, +1).Format("2006.01.02"))
1405+
1406+
const mapping = `{
1407+
"settings":{
1408+
"number_of_shards":1,
1409+
"number_of_replicas":0
1410+
}
1411+
}`
1412+
1413+
// Create indices
1414+
for i, indexName := range []string{indexNameToday, indexNameTomorrow, indexNameYesterday} {
1415+
_, err := client.CreateIndex(indexName).Body(mapping).Do(ctx)
1416+
if err != nil {
1417+
t.Fatal(err)
1418+
}
1419+
defer client.DeleteIndex(indexName).Do(ctx)
1420+
1421+
// Add a document
1422+
id := fmt.Sprintf("%d", i+1)
1423+
_, err = client.Index().Index(indexName).Type("doc").Id(id).BodyJson(map[string]interface{}{
1424+
"index": indexName,
1425+
}).Refresh("wait_for").Do(ctx)
1426+
if err != nil {
1427+
t.Fatal(err)
1428+
}
1429+
}
1430+
1431+
// Count total
1432+
cnt, err := client.
1433+
Count(indexNameYesterday, indexNameToday, indexNameTomorrow).
1434+
Do(ctx)
1435+
if err != nil {
1436+
t.Fatal(err)
1437+
}
1438+
if cnt != 3 {
1439+
t.Fatalf("expected Count=%d; got %d", 3, cnt)
1440+
}
1441+
1442+
// Match all should return all documents
1443+
res, err := client.Search().
1444+
Index("<elastic-trail-{now/d}>", "<elastic-trail-{now-1d/d}>").
1445+
Query(NewMatchAllQuery()).
1446+
Pretty(true).
1447+
Do(ctx)
1448+
if err != nil {
1449+
t.Fatal(err)
1450+
}
1451+
if res.Hits == nil {
1452+
t.Errorf("expected SearchResult.Hits != nil; got nil")
1453+
}
1454+
if got, want := res.Hits.TotalHits, int64(2); got != want {
1455+
t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
1456+
}
1457+
if got, want := len(res.Hits.Hits), 2; got != want {
1458+
t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
1459+
}
1460+
}

setup_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,30 @@ const (
126126
testOrderIndex = "elastic-orders"
127127
testOrderMapping = `
128128
{
129-
"settings":{
130-
"number_of_shards":1,
131-
"number_of_replicas":0
132-
},
133-
"mappings":{
134-
"doc":{
135-
"properties":{
136-
"article":{
137-
"type":"text"
138-
},
139-
"manufacturer":{
140-
"type":"keyword"
141-
},
142-
"price":{
143-
"type":"float"
144-
},
145-
"time":{
146-
"type":"date",
147-
"format": "YYYY-MM-dd"
148-
}
129+
"settings":{
130+
"number_of_shards":1,
131+
"number_of_replicas":0
132+
},
133+
"mappings":{
134+
"doc":{
135+
"properties":{
136+
"article":{
137+
"type":"text"
138+
},
139+
"manufacturer":{
140+
"type":"keyword"
141+
},
142+
"price":{
143+
"type":"float"
144+
},
145+
"time":{
146+
"type":"date",
147+
"format": "YYYY-MM-dd"
149148
}
150149
}
151150
}
152151
}
152+
}
153153
`
154154

155155
/*

0 commit comments

Comments
 (0)