-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.go
163 lines (133 loc) · 3.98 KB
/
es.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
package elastictv
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"time"
"github.com/elastic/go-elasticsearch/v8/esapi"
)
func (estv ElasticTV) queryES(query *Query, index string, doc interface{}) (string, float64, error) {
buf, err := estv.encodeQuery(query)
if err != nil {
return "", 0, err
}
response, err := estv.Client.Search(
estv.Client.Search.WithContext(context.Background()),
estv.Client.Search.WithFrom(0),
estv.Client.Search.WithSize(1),
estv.Client.Search.WithIndex(index),
estv.Client.Search.WithBody(buf),
)
if err != nil {
buf, _ := estv.encodeQuery(query)
return "", 0, fmt.Errorf("error querying elasticsearch: Error: %w Query: %s",
err, buf.String())
}
defer response.Body.Close()
esDoc := esResult{}
if err := json.NewDecoder(response.Body).Decode(&esDoc); err != nil {
return "", 0, fmt.Errorf("error parsing reply: %w", err)
}
if esDoc.Error.Reason != "" {
buf, _ := estv.encodeQuery(query)
return "", 0, fmt.Errorf("error of type [%s] return from elasticsearch: Error %s Query: %s",
esDoc.Error.Type, esDoc.Error.Reason, buf.String())
}
if esDoc.Hits.Total.Value == 0 {
return "", 0, nil
}
if doc != nil {
if err := json.Unmarshal(esDoc.Hits.Hits[0].Source, doc); err != nil {
return "", 0, fmt.Errorf("error parsing source: %w", err)
}
}
return esDoc.Hits.Hits[0].ID, esDoc.Hits.Hits[0].Score, nil
}
func (estv ElasticTV) encodeQuery(query *Query) (*bytes.Buffer, error) {
buf := bytes.Buffer{}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
return nil, fmt.Errorf("error encoding query: %w", err)
}
return &buf, nil
}
func (estv ElasticTV) index(index, docID string, doc interface{}) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(doc); err != nil {
return fmt.Errorf("error encoding document: %w", err)
}
request := esapi.IndexRequest{
Index: index,
DocumentID: docID,
Body: &buf,
Refresh: "false",
}
res, err := request.Do(context.Background(), estv.Client)
if err != nil {
return fmt.Errorf("error indexing document: %w", err)
}
defer res.Body.Close()
if res.IsError() {
response, _ := io.ReadAll(res.Body)
return fmt.Errorf("[%s] Error indexing document ID=%s: %s",
res.Status(), request.DocumentID, string(response))
}
return nil
}
func (estv ElasticTV) RefreshIndices() error {
request := esapi.IndicesRefreshRequest{
Index: []string{
estv.Index.Title,
estv.Index.Episode,
estv.Index.Search,
},
}
res, err := request.Do(context.Background(), estv.Client)
if err != nil {
return fmt.Errorf("error refreshing index: %w", err)
}
defer res.Body.Close()
return nil
}
func (estv ElasticTV) GetRecordID(query *Query, index string) (string, error) {
id, _, err := estv.queryES(query, index, nil)
if err != nil {
return "", err
}
return id, nil
}
func (estv ElasticTV) getRecordWithScore(query *Query, index string, doc interface{}) (float64, error) {
_, score, err := estv.queryES(query, index, doc)
if err != nil {
return 0, err
}
return score, nil
}
func (estv ElasticTV) UpsertTitle(title Title) error {
query := NewQuery().
WithTMDbID(title.IDs.TMDb).
WithIMDbID(title.IDs.IMDb)
recordID, err := estv.GetRecordID(query, estv.Index.Title)
if err != nil {
return err
}
title.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05.0000000")
return estv.index(estv.Index.Title, recordID, title)
}
func (estv ElasticTV) IndexSearchTitle(params SearchTitlesParams) error {
params.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05.0000000")
return estv.index(estv.Index.Search, "", params)
}
func (estv ElasticTV) UpsertEpisode(episode Episode) error {
query := NewQuery().
WithTVShowID(episode.TVShowID).
WithEpisodeNumber(episode.Episode).
WithSeasonNumber(episode.Season)
recordID, err := estv.GetRecordID(query, estv.Index.Episode)
if err != nil {
return err
}
episode.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05.0000000")
return estv.index(estv.Index.Episode, recordID, episode)
}