-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic_search.go
184 lines (162 loc) · 3.65 KB
/
elastic_search.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
package main
// Elastic search client
import (
"context"
"fmt"
"log"
"reflect"
"time"
"github.com/olivere/elastic"
)
const (
indexName = "pages"
indexMapping = `{
"settings":{
"number_of_shards":1,
"number_of_replicas":0,
"analysis": {
"analyzer": {
"clean_html": {
"type": "standard",
"char_filter": ["html_strip"]
}
}
}
},
"mappings":{
"page":{
"properties":{
"title": {
"type": "text"
},
"description": {
"type": "text"
},
"body": {
"type": "text"
},
"url": {
"type": "text"
}
}
}
}
}`
)
var client *elastic.Client
// NewElasticSearchClient returns an elastic seach client
func NewElasticSearchClient() *elastic.Client {
var err error
connected := false
retries := 0
// Custom retry strategy for docker-compose initialization
for connected == false {
// Create a new elastic client
client, err = elastic.NewClient(
elastic.SetURL("http://elasticsearch:9200"), elastic.SetSniff(false))
if err != nil {
// log.Fatal(err)
if retries == 5 {
log.Fatal(err)
}
fmt.Println("Elasticsearch isn't ready for connection", 5-retries, "less")
retries++
time.Sleep(3 * time.Second)
} else {
connected = true
}
}
// Getting the ES version number is quite common, so there's a shortcut
esversion, err := client.ElasticsearchVersion("http://elasticsearch:9200")
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Elasticsearch version %s\n", esversion)
return client
}
// ExistsIndex checks if the given index exists or not
func ExistsIndex(i string) bool {
// Check if index exists
exists, err := client.IndexExists(i).Do(context.TODO())
if err != nil {
log.Fatal(err)
}
return exists
}
// CreateIndex creates a new index
func CreateIndex(i string) {
createIndex, err := client.CreateIndex(indexName).
Body(indexMapping).
Do(context.Background())
if err != nil {
log.Fatal(err)
}
if !createIndex.Acknowledged {
log.Println("CreateIndex was not acknowledged. Check that timeout value is correct.")
}
}
// DeleteIndex in the indexName constant
func DeleteIndex() {
ctx := context.Background()
deleteIndex, err := client.DeleteIndex(indexName).Do(ctx)
if err != nil {
// Handle error
log.Fatal(err)
}
if !deleteIndex.Acknowledged {
log.Println("DeleteIndex was not acknowledged. Check that timeout value is correct.")
}
fmt.Println("Index", indexName, "deleted")
}
// ExistingPage return a boolean and a page if the link is already
// stored in the database
func ExistingPage(link string) (bool, Page) {
var exists bool
var p Page
ctx := context.Background()
// Search for a page in the database using Term Query
// q := elastic.NewTermQuery("url", link)
q := elastic.NewMultiMatchQuery(link, "url")
result, err := client.Search().
Index(indexName).
Query(q).
Do(ctx)
if err != nil {
log.Fatal(err)
}
var ttyp Page
for _, result := range result.Each(reflect.TypeOf(ttyp)) {
page := result.(Page)
if page.URL == link {
exists, p = true, page
return exists, p
}
}
return exists, p
}
// CreatePage adds a new page to the database
func CreatePage(p Page) bool {
ctx := context.Background()
_, err := client.Index().
Index("pages").
Type("page").
Id(p.ID).
BodyJson(p).
Do(ctx)
if err != nil {
fmt.Println(err)
return false
}
return true
}
// UpdatePage adds a new page to the database
func UpdatePage(id string, params map[string]interface{}) bool {
ctx := context.Background()
_, err := client.Update().Index(indexName).Type("page").Id(id).Doc(params).Do(ctx)
if err != nil {
fmt.Println(err)
return false
}
return true
}