This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
forked from maximelamure/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
286 lines (235 loc) · 8.13 KB
/
client_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package elasticsearch_test
import (
"bytes"
"encoding/json"
"testing"
"time"
"github.com/maximelamure/elasticsearch"
)
var (
ProductDocumentType = "PRODUCT"
ESScheme = "http"
ESHost = "localhost"
ESPort = "9200"
ESSearchIndexName = "search"
ESRecommendationIndexName = "recommendation"
IndexName = "test"
IndexMapping = `{"settings":
{
"number_of_shards" : 5,
"number_of_replicas" : 1
}
}`
SuggestionIndexName = "test"
SuggestionIndexMapping = `{
"mappings": {
"suggestion" : {
"properties" : {
"name_suggest" : {
"type" : "completion",
"payloads" : true
}
}
}
}
}`
)
func TestIndexManagement(t *testing.T) {
helper := Test{}
client := elasticsearch.NewClient(ESScheme, ESHost, ESPort)
//If the index exists, remove it
if response, _ := client.IndexExists(IndexName); response {
delReponse, err := client.DeleteIndex(IndexName)
helper.OK(t, err)
helper.Assert(t, delReponse.Acknowledged, "Unable to remove existing index:"+delReponse.Error)
}
//Check if we have the test index
response, err := client.IndexExists(IndexName)
helper.OK(t, err)
helper.Assert(t, !response, "Index has not been removed with the DeleteIndex function")
//Create the index
createResponse, err := client.CreateIndex(IndexName, IndexMapping)
helper.OK(t, err)
helper.Assert(t, createResponse.Acknowledged, "Index has not been created")
//Check if the index has been created
response, err = client.IndexExists(IndexName)
helper.OK(t, err)
helper.Assert(t, response, "Index has not been created with the CreateIndex function")
//Delete the index
deleteResponse, err := client.DeleteIndex(IndexName)
helper.OK(t, err)
helper.Assert(t, deleteResponse.Acknowledged, "Index has not been deleted")
//Check if the index has been deleted
response, err = client.IndexExists(IndexName)
helper.OK(t, err)
helper.Assert(t, !response, "Index has not been removed with DeleteIndex function")
}
func TestCRUD(t *testing.T) {
type Product struct {
Name string
ID string `json:"_id"`
}
helper := Test{}
client := elasticsearch.NewClient(ESScheme, ESHost, ESPort)
//Create the index
client.CreateIndex(IndexName, IndexMapping)
item := Product{Name: "Jeans", ID: "1234"}
jsonProduct, err := json.Marshal(item)
helper.OK(t, err)
//Insert
insertResponse, err := client.InsertDocument(IndexName, ProductDocumentType, item.ID, jsonProduct)
helper.OK(t, err)
helper.Assert(t, insertResponse.ID == "1234", "The document has not been inserted")
version := insertResponse.Version
//Update
item.Name = "Polo"
insertResponse, err = client.InsertDocument(IndexName, ProductDocumentType, item.ID, jsonProduct)
helper.OK(t, err)
helper.Assert(t, insertResponse.Version == version+1, "The document has not been updated")
//Read
readResponse, err := client.Document(IndexName, ProductDocumentType, item.ID)
helper.OK(t, err)
helper.Assert(t, readResponse.Found, "The document has not been found")
var p Product
err = json.Unmarshal(readResponse.Source, &p)
helper.OK(t, err)
helper.Assert(t, p.ID == "1234", "The document has not been retreived")
//Delete
delResponse, err := client.DeleteDocument(IndexName, ProductDocumentType, item.ID)
helper.OK(t, err)
helper.Assert(t, delResponse.Found, "The document has not beem deleted")
//Delete the index
deleteResponse, err := client.DeleteIndex(IndexName)
helper.OK(t, err)
helper.Assert(t, deleteResponse.Acknowledged, "Index has not been deleted")
}
func TestSearch(t *testing.T) {
type Product struct {
Name string
Colors []string
ID string `json:"_id"`
}
products := [...]Product{
Product{Name: "Jeans", ID: "1", Colors: []string{"blue", "red"}},
Product{Name: "Polo", ID: "2", Colors: []string{"yellow", "red"}},
Product{Name: "Shirt", ID: "3", Colors: []string{"brown", "blue"}},
}
helper := Test{}
client := elasticsearch.NewClient(ESScheme, ESHost, ESPort)
client.CreateIndex(IndexName, IndexMapping)
//Bulk
var buffer bytes.Buffer
for _, value := range products {
buffer.WriteString(BulkIndexConstant(IndexName, ProductDocumentType, value.ID))
buffer.WriteByte('\n')
jsonProduct, err := json.Marshal(value)
helper.OK(t, err)
buffer.Write(jsonProduct)
buffer.WriteByte('\n')
}
_, err := client.Bulk(buffer.Bytes())
helper.OK(t, err)
//We have to wait after a bulk
time.Sleep(1500 * time.Millisecond)
//Search
search, err := client.Search(IndexName, ProductDocumentType, SearchByColorQuery("red"), false)
helper.OK(t, err)
helper.Assert(t, search.Hits.Total == 2, "The search doesn't return all matched items")
//MSearch
mqueries := make([]elasticsearch.MSearchQuery, 2)
mqueries[0] = elasticsearch.MSearchQuery{Header: `{ "index":` + IndexName + `, "type":"` + ProductDocumentType + `" }`, Body: `{ "query": {"match_all" : {}}, "from" : 0, "size" : 1} }`}
mqueries[1] = elasticsearch.MSearchQuery{Header: `{ "index":` + IndexName + `, "type":"` + ProductDocumentType + `" }`, Body: `{"query": {"match_all" : {}}, "from" : 0, "size" : 2}}`}
msresult, err := client.MSearch(mqueries)
helper.OK(t, err)
helper.Assert(t, msresult.Responses[0].Hits.Total == 1, "The msearch doesn't return all matched items")
helper.Assert(t, msresult.Responses[1].Hits.Total == 2, "The msearch doesn't return all matched items")
//Delete the index
deleteResponse, err := client.DeleteIndex(IndexName)
helper.OK(t, err)
helper.Assert(t, deleteResponse.Acknowledged, "Index has not been deleted")
}
func BulkIndexConstant(indexName, documentType, id string) string {
return `{"index":
{ "_index": "` + indexName + `",
"_type": "` + documentType + `",
"_id": "` + id + `"
}
}`
}
func SearchByColorQuery(color string) string {
return `{
"query": {
"match": {
"Colors": "` + color + `"
}
}
}`
}
func TestSuggestion(t *testing.T) {
type PayLoadSuggester struct {
ID string `json:"id"`
SKU string `json:"sku"`
}
type InputSuggester struct {
Input []string `json:"input"`
Ouput string `json:"output"`
Payload PayLoadSuggester `json:"payload"`
}
type SuggestionItem struct {
Name InputSuggester `json:"name_suggest"`
}
type OutputSuggester struct {
Text string `json:"text"`
Score float32 `json:"score"`
Payload PayLoadSuggester `json:"payload"`
}
type SuggestionResult struct {
Shards struct {
Total int `json:"total"`
Successful int `json:"successful"`
Failed int `json:"failed"`
} `json:"_shards"`
Suggestion []struct {
Text string `json:"text"`
Offset float32 `json:"offset"`
Lenght int `json:"length"`
Options []OutputSuggester `json:"options"`
} `json:"suggestion"`
}
helper := Test{}
client := elasticsearch.NewClient(ESScheme, ESHost, ESPort)
client.CreateIndex(SuggestionIndexName, SuggestionIndexMapping)
//Add Data
sugg := &SuggestionItem{}
sugg.Name = InputSuggester{}
sugg.Name.Input = []string{"jeans", "Levi's jeans", "Levi's"}
sugg.Name.Ouput = "Levi's jeans"
sugg.Name.Payload = PayLoadSuggester{"12345", "HJYSTG"}
jsonSuggestion, err := json.Marshal(sugg)
helper.OK(t, err)
//Insert
insertResponse, err := client.InsertDocument(SuggestionIndexName, "suggestion", "1234", jsonSuggestion)
helper.OK(t, err)
helper.Assert(t, insertResponse.ID == "1234", "The document has not been inserted")
//Suggest
suggestResponse, err := client.Suggest(SuggestionIndexName, SuggestByTermQuery("jean"))
helper.OK(t, err)
var s SuggestionResult
err = json.Unmarshal(suggestResponse, &s)
helper.OK(t, err)
helper.Assert(t, s.Shards.Failed == 0, "No suggestion inserted")
//Delete the index
deleteResponse, err := client.DeleteIndex(SuggestionIndexName)
helper.OK(t, err)
helper.Assert(t, deleteResponse.Acknowledged, "Index has not been deleted")
}
func SuggestByTermQuery(term string) string {
return `{
"suggestion" : {
"text" : "` + term + `",
"completion" : {
"field" : "name_suggest"
}
}
}`
}