-
Notifications
You must be signed in to change notification settings - Fork 684
/
es_test.go
167 lines (150 loc) · 3.36 KB
/
es_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package es
import (
"context"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
"os"
"reflect"
"testing"
"time"
)
var client *elastic.Client
func TestMain(m *testing.M) {
var err error
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://172.13.3.160:9200/"))
if err != nil {
panic(err)
}
info, code, err := client.Ping("http://172.13.3.160:9200/").Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
esversion, err := client.ElasticsearchVersion("http://172.13.3.160:9200/")
if err != nil {
panic(err)
}
fmt.Printf("Elasticsearch version %s\n", esversion)
os.Exit(m.Run())
}
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int `json:"age"`
}
var indexName = "hwholiday"
func getId() string {
return fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
}
func TestCreate(t *testing.T) {
res, err := client.Index().Index(indexName).Id("1").BodyJson(User{
FirstName: "a",
LastName: "b",
Age: 10,
}).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Println(res)
res, err = client.Index().Index(indexName).Id("2").BodyJson(User{
FirstName: "a",
LastName: "c",
Age: 20,
}).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Println(res)
res, err = client.Index().Index(indexName).Id("3").BodyJson(User{
FirstName: "abcd e",
LastName: "f",
Age: 30,
}).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Println(res)
}
func TestUpdate(t *testing.T) {
res, err := client.Update().Index(indexName).Id("1").Doc(map[string]interface{}{
"age": 50,
"last_name": "c",
}).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Println(res)
}
func TestGet(t *testing.T) {
res, err := client.Get().Index(indexName).Id("1").Do(context.Background())
if err != nil {
panic(err)
}
var data User
err = json.Unmarshal(res.Source, &data)
if err != nil {
panic(err)
}
fmt.Println(data)
}
func TestDel(t *testing.T) {
res, err := client.Delete().Index(indexName).Id("1").Do(context.Background())
if err != nil {
panic(err)
}
fmt.Println(res)
}
func TestInsertArray(t *testing.T) {
res, err := client.Search(indexName).Do(context.Background())
if err != nil {
panic(err)
}
for _, v := range res.Each(reflect.TypeOf(User{})) {
t := v.(User)
fmt.Println(t)
}
}
func TestQuery(t *testing.T) {
//字段相等
q := elastic.NewQueryStringQuery("last_name:c")
printQuery(q)
//条件查询
bQ := elastic.NewBoolQuery()
bQ.Must(elastic.NewMatchQuery("last_name", "c"))
bQ.Filter(elastic.NewRangeQuery("age").Gt(10))
printQuery(bQ)
//短语搜索 搜索 first_name字段中有 a
mQ := elastic.NewMatchPhraseQuery("first_name", "e")
printQuery(mQ)
}
func TestList(t *testing.T) {
var (
size = 10
page = 1
)
if size < 0 || page < 1 {
return
}
res, err := client.Search(indexName).
Size(size).
From((page - 1) * size).
Do(context.Background())
if err != nil {
panic(err)
}
for _, v := range res.Each(reflect.TypeOf(User{})) {
t := v.(User)
fmt.Println(t)
}
}
func printQuery(q elastic.Query) {
res, err := client.Search(indexName).Query(q).Do(context.Background())
if err != nil {
panic(err)
}
for _, v := range res.Each(reflect.TypeOf(User{})) {
t := v.(User)
fmt.Println(t)
}
}