forked from TechPrimers/docker-mysql-spring-boot-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (70 loc) · 1.99 KB
/
main.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
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"github.com/elastic/go-elasticsearch/v7"
)
func main() {
log.SetFlags(0)
esHost := "http://localhost:9200"
indexName := "so1"
searchString := "Handbill not printed"
var r map[string]interface{}
cfg := elasticsearch.Config{Addresses: []string{
esHost,
},
Username: "user",
Password: "pass",
}
es, err := elasticsearch.NewClient(cfg)
// Exit the system if connection raises an error
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
var buf bytes.Buffer
query := map[string]interface{}{
"query": map[string]interface{}{
"query_string": map[string]interface{}{
"query": searchString,
},
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}
res, err := es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex(indexName),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
defer res.Body.Close()
if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
} else {
// Print the response status and error information.
log.Fatalf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
log.Fatalf("Error parsing the response body: %s", err)
}
hits := int(r["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64))
log.Printf("Number of hits: %d", hits)
// Print the ID and document source for each hit.
for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"])
}
}