@@ -7,6 +7,7 @@ package elastic
7
7
import (
8
8
"context"
9
9
"encoding/json"
10
+ "fmt"
10
11
"reflect"
11
12
"testing"
12
13
"time"
@@ -1392,3 +1393,68 @@ func TestSearchWithDocvalueFields(t *testing.T) {
1392
1393
}
1393
1394
}
1394
1395
}
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
+ }
0 commit comments