/
elasticsearch.go
576 lines (495 loc) · 14.9 KB
/
elasticsearch.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package stats
import (
"bytes"
"context"
"encoding/json"
"github.com/hscells/cqr"
gpipeline "github.com/hscells/groove/pipeline"
"github.com/hscells/transmute/backend"
"github.com/hscells/transmute/lexer"
"github.com/hscells/transmute/parser"
tpipeline "github.com/hscells/transmute/pipeline"
"github.com/hscells/trecresults"
"gopkg.in/olivere/elastic.v5"
"io"
"log"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
// ElasticsearchStatisticsSource is a way of gathering statistics for a collection using Elasticsearch.
type ElasticsearchStatisticsSource struct {
client *elastic.Client
documentType string
index string
options SearchOptions
parameters map[string]float64
Scroll bool
Analyser string
AnalyseField string
wg sync.WaitGroup
}
// SearchOptions gets the immutable execute options for the statistics source.
func (es *ElasticsearchStatisticsSource) SearchOptions() SearchOptions {
return es.options
}
// Parameters gets the immutable parameters for the statistics source.
func (es *ElasticsearchStatisticsSource) Parameters() map[string]float64 {
return es.parameters
}
// TermFrequency is the term frequency in the field.
func (es *ElasticsearchStatisticsSource) TermFrequency(term, field, document string) (float64, error) {
resp, err := es.client.TermVectors(es.index, es.documentType).Id(document).Do(context.Background())
if err != nil {
return 0, err
}
if tv, ok := resp.TermVectors[field]; ok {
return float64(tv.Terms[term].TermFreq), nil
}
return 0.0, nil
}
// DocumentFrequency is the document frequency (the number of documents containing the current term).
func (es *ElasticsearchStatisticsSource) DocumentFrequency(term string, field string) (float64, error) {
resp, err := es.client.TermVectors(es.index, es.documentType).
Doc(map[string]string{field: term}).
FieldStatistics(false).
TermStatistics(true).
Offsets(false).
Positions(false).
Payloads(false).
Fields(field).
PerFieldAnalyzer(map[string]string{field: ""}).
Do(context.Background())
if err != nil {
return 0, err
}
if tv, ok := resp.TermVectors[field]; ok {
return float64(tv.Terms[term].DocFreq), nil
}
return 0.0, nil
}
// TotalTermFrequency is a sum of total term frequencies (the sum of total term frequencies of each term in this field).
func (es *ElasticsearchStatisticsSource) TotalTermFrequency(term, field string) (float64, error) {
docField := strings.Replace(field, es.AnalyseField, "", -1)
req := es.client.TermVectors(es.index, es.documentType).
Doc(map[string]string{field: term}).
TermStatistics(true).
Offsets(false).
Positions(false).
Payloads(false)
if strings.ContainsRune(term, '*') {
req = req.PerFieldAnalyzer(map[string]string{docField: "medline_analyser"})
}
resp, err := req.Do(context.Background())
if err != nil {
return 0.0, err
}
if tv, ok := resp.TermVectors[field]; ok {
t := strings.ToLower(strings.Replace(strings.Replace(strings.Replace(term, "\"", "", -1), "*", "", -1), "~", "", -1))
return float64(tv.Terms[t].Ttf), nil
}
return 0.0, nil
}
// InverseDocumentFrequency is the ratio of of documents in the collection to the number of documents the term appears
// in, logarithmically smoothed.
func (es *ElasticsearchStatisticsSource) InverseDocumentFrequency(term, field string) (float64, error) {
resp1, err := es.client.IndexStats(es.index).Do(context.Background())
if err != nil {
return 0.0, err
}
N := resp1.All.Total.Docs.Count
docField := strings.Replace(field, es.AnalyseField, "", -1)
req := es.client.TermVectors(es.index, es.documentType).
Doc(map[string]string{docField: term}).
FieldStatistics(false).
TermStatistics(true).
Offsets(false).
Positions(false).
Pretty(false).
Payloads(false)
if strings.ContainsRune(term, '*') {
req = req.PerFieldAnalyzer(map[string]string{docField: "medline_analyser"})
}
resp2, err := req.Do(context.Background())
if err != nil {
return 0.0, err
}
if tv, ok := resp2.TermVectors[field]; ok {
nt := tv.Terms[term].DocFreq
if nt == 0 {
return 0.0, nil
}
return idf(float64(N), float64(nt)), nil
}
return 0.0, nil
}
// VocabularySize is the total number of terms in the vocabulary.
func (es *ElasticsearchStatisticsSource) VocabularySize(field string) (float64, error) {
resp, err := es.client.TermVectors(es.index, es.documentType).
Doc(map[string]string{field: ""}).
Offsets(false).
Positions(false).
Realtime(false).
Pretty(false).
Payloads(false).
Fields(field).
PerFieldAnalyzer(map[string]string{field: ""}).
Do(context.Background())
if err != nil {
return 0.0, err
}
return float64(resp.TermVectors[field].FieldStatistics.SumTtf), nil
}
// RetrievalSize is the minimum number of documents that contains at least one of the query terms.
func (es *ElasticsearchStatisticsSource) RetrievalSize(query cqr.CommonQueryRepresentation) (float64, error) {
// Transform the query to an Elasticsearch query.
q, err := toElasticsearch(query)
if err != nil {
return 0.0, err
}
// Only then can we issue it to Elasticsearch using our API.
result, err := es.client.Count(es.index).
Query(elastic.NewRawStringQuery(q)).
Do(context.Background())
if err != nil {
return 0.0, err
}
return float64(result), nil
}
// TermVector retrieves the term vector for a document.
func (es *ElasticsearchStatisticsSource) TermVector(document string) (TermVector, error) {
tv := TermVector{}
req := es.client.TermVectors(es.index, es.documentType).
Id(document).
FieldStatistics(true).
TermStatistics(true).
Offsets(false).
Pretty(false).
Positions(false).
Payloads(false).
Fields("*")
resp, err := req.Do(context.Background())
if err != nil {
return tv, err
}
for field, vector := range resp.TermVectors {
for term, vec := range vector.Terms {
tv = append(tv, TermVectorTerm{
Term: term,
Field: field,
DocumentFrequency: float64(vec.DocFreq),
TermFrequency: float64(vec.TermFreq),
TotalTermFrequency: float64(vec.Ttf),
})
}
}
return tv, nil
}
// ExecuteFast executes an Elasticsearch query and retrieves only the document ids in the fastest possible way. Do not
// use this for ranked results as the concurrency of this method does not guarantee order.
func (es *ElasticsearchStatisticsSource) ExecuteFast(query gpipeline.Query, options SearchOptions) ([]uint32, error) {
// Transform the query to an Elasticsearch query.
q, err := toElasticsearch(query.Query)
if err != nil {
return nil, err
}
// Set the Limit to how many goroutines can be run.
// http://jmoiron.net/blog/limiting-concurrency-in-go/
concurrency := runtime.NumCPU()
sem := make(chan bool, concurrency)
hits := make([][]uint32, concurrency)
log.Println("executing as fast as possible with Elasticsearch", query.Query)
for i := 0; i < concurrency; i++ {
sem <- true
go func(n int) {
defer func() { <-sem }()
search:
hits[n] = []uint32{}
// Scroll execute.
svc := es.client.Scroll(es.index).
FetchSource(false).
Pretty(false).
Type(es.documentType).
KeepAlive("10m").
Slice(elastic.NewSliceQuery().Id(n).Max(concurrency)).
SearchSource(
elastic.NewSearchSource().
NoStoredFields().
FetchSource(false).
Size(options.Size).
Slice(elastic.NewSliceQuery().Id(n).Max(concurrency)).
TrackScores(false).
Query(elastic.NewRawStringQuery(q)))
for {
result, err := svc.Do(context.Background())
if err == io.EOF {
break
}
if elastic.IsConnErr(err) {
log.Println(err)
log.Println("retrying...")
goto search
}
if err != nil {
log.Println(err)
return
}
for _, hit := range result.Hits.Hits {
id, err := strconv.Atoi(hit.Id)
if err != nil {
log.Println(err)
return
}
hits[n] = append(hits[n], uint32(id))
}
log.Printf("%v: %v/%v\n", n, len(hits[n]), result.Hits.TotalHits)
}
err = svc.Clear(context.Background())
if err != nil {
log.Println(err)
//panic(err)
return
}
}(i)
}
// Wait until the last goroutine has read from the semaphore.
for i := 0; i < cap(sem); i++ {
sem <- true
}
var results []uint32
for _, hit := range hits {
results = append(results, hit...)
}
log.Printf("done, %v results in total\n", len(results))
return results, nil
}
// Execute runs the query on Elasticsearch and returns results in trec format.
func (es *ElasticsearchStatisticsSource) Execute(query gpipeline.Query, options SearchOptions) (trecresults.ResultList, error) {
// Transform the query to an Elasticsearch query.
q, err := toElasticsearch(query.Query)
if err != nil {
return nil, err
}
// Only then can we issue it to Elasticsearch using our API.
if es.Scroll {
var hits []*elastic.SearchHit
// Scroll execute.
svc := es.client.Scroll(es.index).
FetchSource(false).
Pretty(false).
Type(es.documentType).
KeepAlive("30m").
SearchSource(
elastic.NewSearchSource().
NoStoredFields().
FetchSource(false).
Size(options.Size).
TrackScores(false).
Query(elastic.NewRawStringQuery(q)))
for {
result, err := svc.Do(context.Background())
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
hits = append(hits, result.Hits.Hits...)
}
err = svc.Clear(context.Background())
if err != nil {
return nil, err
}
log.Println(len(hits))
// Block until all the channels have completed.
results := make(trecresults.ResultList, len(hits))
for i, hit := range hits {
results[i] = &trecresults.Result{
Topic: query.Topic,
Iteration: "Q0",
DocId: hit.Id,
Rank: int64(i),
Score: *hit.Score,
RunName: options.RunName,
}
}
return results, nil
}
// Regular execute.
result, err := es.client.Search(es.index).
Index(es.index).
Type(es.documentType).
Query(elastic.NewRawStringQuery(q)).
Size(options.Size).
NoStoredFields().
Do(context.Background())
if err != nil {
return nil, err
}
// Construct the results from the Elasticsearch hits.
N := len(result.Hits.Hits)
results := make(trecresults.ResultList, N)
for i, hit := range result.Hits.Hits {
results[i] = &trecresults.Result{
Topic: query.Topic,
Iteration: "Q0",
DocId: hit.Id,
Rank: int64(i),
Score: *hit.Score,
RunName: options.RunName,
}
}
return results, nil
}
func (es *ElasticsearchStatisticsSource) CollectionSize() (float64, error) {
panic("implement me")
}
// Analyse is a specific Elasticsearch method used in the analyse transformation.
func (es *ElasticsearchStatisticsSource) Analyse(text, analyser string) (tokens []string, err error) {
res, err := es.client.IndexAnalyze().Index(es.index).Analyzer(analyser).Text(text).Do(context.Background())
if err != nil {
return
}
for _, token := range res.Tokens {
tokens = append(tokens, token.Token)
}
return
}
// toElasticsearch transforms a cqr query into an Elasticsearch query.
func toElasticsearch(query cqr.CommonQueryRepresentation) (string, error) {
var result map[string]interface{}
// For a Boolean query, it gets a little tricky.
// First we need to get the string representation of the cqr.
repr, err := backend.NewCQRQuery(query).StringPretty()
if err != nil {
return "", err
}
// Then we need to compile it into an Elasticsearch query.
p := tpipeline.NewPipeline(
parser.NewCQRParser(),
backend.NewElasticsearchCompiler(),
tpipeline.TransmutePipelineOptions{
LexOptions: lexer.LexOptions{
FormatParenthesis: true,
},
RequiresLexing: false,
})
esQuery, err := p.Execute(repr)
if err != nil {
return "", err
}
// After that, we need to unmarshal it to get the underlying structure.
var tmpQuery map[string]interface{}
s, err := esQuery.String()
if err != nil {
return "", err
}
err = json.Unmarshal(bytes.NewBufferString(s).Bytes(), &tmpQuery)
if err != nil {
return "", err
}
result = tmpQuery["query"].(map[string]interface{})
b, err := json.Marshal(result)
if err != nil {
return "", err
}
return bytes.NewBuffer(b).String(), nil
}
// ElasticsearchHosts sets the hosts for the Elasticsearch client.
func ElasticsearchHosts(hosts ...string) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
var err error
if len(hosts) == 0 {
es.client, err = elastic.NewClient(elastic.SetURL("http://localhost:9200"))
if err != nil {
panic(err)
}
} else {
sniff := true
for _, u := range hosts {
if strings.Contains(u, "localhost") {
sniff = false
break
}
}
es.client, err = elastic.NewClient(
elastic.SetURL(hosts...),
elastic.SetSniff(sniff),
elastic.SetHealthcheck(false),
elastic.SetHealthcheckTimeout(time.Hour))
if err != nil {
panic(err)
}
}
return
}
}
// ElasticsearchDocumentType sets the document type for the Elasticsearch client.
func ElasticsearchDocumentType(documentType string) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.documentType = documentType
return
}
}
// ElasticsearchIndex sets the index for the Elasticsearch client.
func ElasticsearchIndex(index string) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.index = index
return
}
}
// ElasticsearchSearchOptions sets the execute options for the statistic source.
func ElasticsearchSearchOptions(options SearchOptions) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.options = options
return
}
}
// ElasticsearchParameters sets the parameters for the statistic source.
func ElasticsearchParameters(params map[string]float64) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.parameters = params
return
}
}
// ElasticsearchAnalyser sets the analyser for the statistic source.
func ElasticsearchAnalyser(analyser string) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.Analyser = analyser
return
}
}
// ElasticsearchAnalysedField sets the analyser for the statistic source.
func ElasticsearchAnalysedField(field string) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.AnalyseField = field
return
}
}
// ElasticsearchScroll sets the scroll for the statistic source.
func ElasticsearchScroll(scroll bool) func(*ElasticsearchStatisticsSource) {
return func(es *ElasticsearchStatisticsSource) {
es.Scroll = scroll
return
}
}
// NewElasticsearchStatisticsSource creates a new ElasticsearchStatisticsSource using functional options.
func NewElasticsearchStatisticsSource(options ...func(*ElasticsearchStatisticsSource)) (*ElasticsearchStatisticsSource, error) {
es := &ElasticsearchStatisticsSource{}
if len(options) == 0 {
var err error
es.client, err = elastic.NewClient(elastic.SetURL("http://localhost:9200"),
elastic.SetSniff(false),
elastic.SetHealthcheckTimeout(1*time.Hour))
if err != nil {
return nil, err
}
} else {
for _, option := range options {
option(es)
}
}
return es, nil
}