Debian Codegraph @

count_test.go

raw
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 "testing"
10 )
11
12 func TestCountURL(t *testing.T) {
13 client := setupTestClientAndCreateIndex(t)
14
15 tests := []struct {
16 Indices []string
17 Types []string
18 Expected string
19 }{
20 {
21 []string{},
22 []string{},
23 "/_all/_count",
24 },
25 {
26 []string{},
27 []string{"tweet"},
28 "/_all/tweet/_count",
29 },
30 {
31 []string{"twitter-*"},
32 []string{"tweet", "follower"},
33 "/twitter-%2A/tweet%2Cfollower/_count",
34 },
35 {
36 []string{"twitter-2014", "twitter-2015"},
37 []string{"tweet", "follower"},
38 "/twitter-2014%2Ctwitter-2015/tweet%2Cfollower/_count",
39 },
40 }
41
42 for _, test := range tests {
43 path, _, err := client.Count().Index(test.Indices...).Type(test.Types...).buildURL()
44 if err != nil {
45 t.Fatal(err)
46 }
47 if path != test.Expected {
48 t.Errorf("expected %q; got: %q", test.Expected, path)
49 }
50 }
51 }
52
53 func TestCount(t *testing.T) {
54 client := setupTestClientAndCreateIndex(t)
55
56 tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
57 tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
58 tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
59
60 // Add all documents
61 _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
62 if err != nil {
63 t.Fatal(err)
64 }
65
66 _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
72 if err != nil {
73 t.Fatal(err)
74 }
75
76 _, err = client.Flush().Index(testIndexName).Do(context.TODO())
77 if err != nil {
78 t.Fatal(err)
79 }
80
81 // Count documents
82 count, err := client.Count(testIndexName).Do(context.TODO())
83 if err != nil {
84 t.Fatal(err)
85 }
86 if count != 3 {
87 t.Errorf("expected Count = %d; got %d", 3, count)
88 }
89
90 // Count documents
91 count, err = client.Count(testIndexName).Type("tweet").Do(context.TODO())
92 if err != nil {
93 t.Fatal(err)
94 }
95 if count != 3 {
96 t.Errorf("expected Count = %d; got %d", 3, count)
97 }
98
99 // Count documents
100 count, err = client.Count(testIndexName).Type("gezwitscher").Do(context.TODO())
101 if err != nil {
102 t.Fatal(err)
103 }
104 if count != 0 {
105 t.Errorf("expected Count = %d; got %d", 0, count)
106 }
107
108 // Count with query
109 query := NewTermQuery("user", "olivere")
110 count, err = client.Count(testIndexName).Query(query).Do(context.TODO())
111 if err != nil {
112 t.Fatal(err)
113 }
114 if count != 2 {
115 t.Errorf("expected Count = %d; got %d", 2, count)
116 }
117
118 // Count with query and type
119 query = NewTermQuery("user", "olivere")
120 count, err = client.Count(testIndexName).Type("tweet").Query(query).Do(context.TODO())
121 if err != nil {
122 t.Fatal(err)
123 }
124 if count != 2 {
125 t.Errorf("expected Count = %d; got %d", 2, count)
126 }
127 }