-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexAPI.go
198 lines (156 loc) · 5.05 KB
/
indexAPI.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
package main
import (
"fmt"
"log"
"strconv"
"github.com/grimmer0125/elastic"
// elastigo "github.com/mattbaird/elastigo/lib"
)
// no use now, for testing
// type Tweet struct {
// User string `json:"user"`
// Message string `json:"message"`
// }
const (
awsURL = "https://search-search2-rm2lnw4mm2i4oz3ehs3pgpk3o4.us-west-2.es.amazonaws.com"
githubIndex = "githubrepos" //can not be githubRepos, should lower case !!
)
func initElastic() (client *elastic.Client) {
//Create a client
client, err := elastic.NewClient(
elastic.SetURL(awsURL),
// elastic.SetHttpClient(http.DefaultClient),
elastic.SetHealthcheck(false),
elastic.SetSniff(false))
if err != nil {
// Handle error
log.Println("new elastic Search fail: ", err)
return nil
}
return client
}
func useScrollToDelete(client *elastic.Client, account string) {
q := elastic.NewMatchQuery("starredBy", account)
searchResult, err := client.Scroll(githubIndex).Size(10000).Query(q).Do()
bulkRequest := client.Bulk()
if err != nil {
// Handle error
log.Println("new elastic Scroll fail: ", err)
} else {
log.Println("get scroll result:")
if searchResult.Hits.TotalHits > 0 {
fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)
// Iterate through results
for _, hit := range searchResult.Hits.Hits {
// fmt.Println("got id:", hit.Id)
request := elastic.NewBulkDeleteRequest().Index(githubIndex).Type(account).Id(hit.Id)
bulkRequest = bulkRequest.Add(request)
}
} else {
// No hits
fmt.Print("Found no hits in %s\n", account)
}
}
log.Println("numbe of delete requests:", bulkRequest.NumberOfActions())
bulkResponse, err := bulkRequest.Do()
if err != nil {
// get bulk error: elastic: No bulk actions to commit !!!!
log.Println("get bulk error:", err)
// t.Fatal(err)
return
}
log.Println("after, numbe of delete requests:", bulkRequest.NumberOfActions())
if bulkResponse == nil {
log.Println("expected bulkResponse to be != nil; got nil")
} else {
log.Println("buld resp ok")
}
log.Println("use elasticsearch done")
// use this result to delete !!!!!!!!!!!!!!
//ref 1. https://github.com/olivere/elastic/blob/e4233aab21b455148600a8da2172576082ad8125/scroll_test.go
// 2. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
}
func SendToGitHubElasticsearch(repoList []*GitHubRepo, account string) error {
client := initElastic()
if client == nil {
// Handle error
log.Println("new elastic Search fail")
return nil
}
// Delete the content under the account/type
useScrollToDelete(client, account)
// Create an index
// _, err = client.CreateIndex(githubIndex).Do()
// if err != nil {
// // Handle error
// // panic(err)
// log.Println("create index fail :", err)
// }
// Add a document to the index
// tweet := Tweet{User: "olivere", Message: "Take Five"}
// _, err = client.Index().
// Index("twitter789").
// Type("tweet").
// Id("1").
// BodyJson(tweet).
// Refresh(true).
// Do()
// if err != nil {
// // Handle error
// // panic(err)
// log.Println("index fail,", err)
// }
// Delete the index again
// log.Println("try to delete first")
// _, err := client.DeleteIndex(githubIndex).Do()
// if err != nil {
// // Handle error
// log.Println("delete first fail:", err)
// if err.Error() == "elastic: Error 404 (Not Found): no such index [type=index_not_found_exception]" {
// log.Println("just not found index")
// } else {
// return err
// }
// }
// bulk - add
var err error
if len(repoList) > 0 {
bulkRequest := client.Bulk()
for i, repo := range repoList {
objectID := strconv.Itoa(i)
// log.Println("repo:", *repo)
index := elastic.NewBulkIndexRequest().Index(githubIndex).Type(account).Id(objectID).Doc(*repo)
bulkRequest = bulkRequest.Add(index)
}
// tweet3 := make(map[string]interface{})
// tweet3["user2"] = "x21"
// tweet3["message2"] = "x22"
// tweet3 := map[string]interface{}
//tweet1 := GitHubRepo{"1", "1", "1", "1", "1", "1", "1", "1"}
// tweet1 := Tweet{User: "olivere22", Message: "Welcome to Golang and Elasticsearch."}
// tweet2 := Tweet{User: "sandrae22", Message: "Dancing all night long. Yeah."}
// index1Req := elastic.NewBulkIndexRequest().Index("repo").Type("tweet").Id("91").Doc(tweet1)
// index2Req := elastic.NewBulkIndexRequest().Index("twitter1271").Type("tweet").Id("3").Doc(tweet3)
// // // bulkRequest := client.Bulk()
// bulkRequest = bulkRequest.Add(index1Req)
// bulkRequest = bulkRequest.Add(index2Req)
log.Println("numbe of add requests:", bulkRequest.NumberOfActions())
bulkResponse, err := bulkRequest.Do()
if err != nil {
log.Println("get bulk error:", err)
// t.Fatal(err)
return err
}
log.Println("after, number of add requests:", bulkRequest.NumberOfActions())
if bulkResponse == nil {
log.Println("expected bulkResponse to be != nil; got nil")
} else {
log.Println("buld resp ok")
}
} else {
log.Println("repo list is empty, so directly return error nil")
err = nil
}
log.Println("use elasticsearch done")
return err
}