forked from Restream/reindexer
/
elastic.go
174 lines (151 loc) · 3.98 KB
/
elastic.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
package repo
import (
"context"
"log"
"math/rand"
"net"
"net/http"
"reflect"
"strconv"
"time"
elastic "gopkg.in/olivere/elastic.v5"
)
type ElasticRepo struct {
db *elastic.Client
}
func (repo *ElasticRepo) Init() bool {
var err error
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
if repo.db, err = elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"), elastic.SetHttpClient(httpClient)); err != nil {
panic(err)
}
return true
}
func (repo *ElasticRepo) Seed(itemsInDataSet int) bool {
log.Printf("Seeding data to Elastic")
ctx := context.Background()
repo.db.DeleteIndex("items").Do(ctx)
if _, err := repo.db.CreateIndex("items").Do(ctx); err != nil {
panic(err)
}
bulkRequest := repo.db.Bulk()
for i := 0; i < itemsInDataSet; i++ {
bulkRequest = bulkRequest.Add(elastic.NewBulkIndexRequest().Index("items").Type("items").Id(strconv.Itoa(i)).Doc(newItem(i)))
if (i%10000) == 0 || i == itemsInDataSet-1 {
if _, err := bulkRequest.Do(ctx); err != nil {
panic(err)
}
bulkRequest = repo.db.Bulk()
}
}
return true
}
func (repo *ElasticRepo) QueryFullText(textQuery func() string, N int, limit int) (ret []*Item) {
ctx := context.Background()
ret = make([]*Item, 0, limit)
for i := 0; i < N; i++ {
was := false
if searchResult, err := repo.db.Search().Index("items").
Query(elastic.NewQueryStringQuery(textQuery()).DefaultField("description").DefaultOperator("OR")).
From(0).Size(limit).Do(ctx); err != nil {
panic(err)
} else {
for _, item := range searchResult.Each(reflect.TypeOf(Item{})) {
it := item.(Item)
ret = append(ret, &it)
was = true
}
if !was {
// panic(0)
}
}
}
return ret
}
func (repo *ElasticRepo) QueryByID(N int, onlyQuery bool) (it *Item) {
ctx := context.Background()
for i := 0; i < N; i++ {
if searchResult, err := repo.db.Search().Index("items").
Query(elastic.NewTermQuery("id", strconv.Itoa(rand.Int()%itemsInDataSet))).
From(0).Size(1).Do(ctx); err != nil {
panic(err)
} else if !onlyQuery {
for _, item := range searchResult.Each(reflect.TypeOf(Item{})) {
pit := item.(Item)
it = &pit
break
}
}
}
return it
}
func (repo *ElasticRepo) Query2Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
ctx := context.Background()
if !onlyQuery {
ret = make([]*Item, 0, limit)
}
for i := 0; i < N; i++ {
if searchResult, err := repo.db.Search().Index("items").
Query(
elastic.NewBoolQuery().Filter(
elastic.NewRangeQuery("year").Gt("2010"),
elastic.NewMatchQuery("name", randString()),
),
).From(0).Size(limit).Do(ctx); err != nil {
panic(err)
} else {
for _, item := range searchResult.Each(reflect.TypeOf(Item{})) {
it := item.(Item)
ret = append(ret, &it)
}
}
}
return ret
}
func (repo *ElasticRepo) Query1Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
ctx := context.Background()
if !onlyQuery {
ret = make([]*Item, 0, limit)
}
for i := 0; i < N; i++ {
if searchResult, err := repo.db.Search().Index("items").
Query(elastic.NewRangeQuery("year").Gt("2010")).
From(0).Size(limit).Do(ctx); err != nil {
panic(err)
} else if !onlyQuery {
for _, item := range searchResult.Each(reflect.TypeOf(Item{})) {
it := item.(Item)
ret = append(ret, &it)
}
}
}
return ret
}
func (repo *ElasticRepo) Update(N int) {
ctx := context.Background()
for i := 0; i < N; i++ {
id := rand.Int() % itemsInDataSet
item := newItem(id)
_, err := repo.db.Index().Index("items").Type("items").Id(strconv.Itoa(id)).BodyJson(*item).Do(ctx)
if err != nil {
panic(err)
}
}
}
func init() {
registerRepo("elastic", &ElasticRepo{})
}