forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_searchquery.go
313 lines (272 loc) · 8.16 KB
/
cluster_searchquery.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
package gocb
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/opentracing/opentracing-go"
"gopkg.in/couchbaselabs/jsonx.v1"
)
// SearchResultLocation holds the location of a hit in a list of search results.
type SearchResultLocation struct {
Position int `json:"position,omitempty"`
Start int `json:"start,omitempty"`
End int `json:"end,omitempty"`
ArrayPositions []uint `json:"array_positions,omitempty"`
}
// SearchResultHit holds a single hit in a list of search results.
type SearchResultHit struct {
Index string `json:"index,omitempty"`
Id string `json:"id,omitempty"`
Score float64 `json:"score,omitempty"`
Explanation map[string]interface{} `json:"explanation,omitempty"`
Locations map[string]map[string][]SearchResultLocation `json:"locations,omitempty"`
Fragments map[string][]string `json:"fragments,omitempty"`
Fields map[string]string `json:"fields,omitempty"`
}
// SearchResultTermFacet holds the results of a term facet in search results.
type SearchResultTermFacet struct {
Term string `json:"term,omitempty"`
Count int `json:"count,omitempty"`
}
// SearchResultNumericFacet holds the results of a numeric facet in search results.
type SearchResultNumericFacet struct {
Name string `json:"name,omitempty"`
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
Count int `json:"count,omitempty"`
}
// SearchResultDateFacet holds the results of a date facet in search results.
type SearchResultDateFacet struct {
Name string `json:"name,omitempty"`
Min string `json:"min,omitempty"`
Max string `json:"max,omitempty"`
Count int `json:"count,omitempty"`
}
// SearchResultFacet holds the results of a specified facet in search results.
type SearchResultFacet struct {
Field string `json:"field,omitempty"`
Total int `json:"total,omitempty"`
Missing int `json:"missing,omitempty"`
Other int `json:"other,omitempty"`
Terms []SearchResultTermFacet `json:"terms,omitempty"`
NumericRanges []SearchResultNumericFacet `json:"numeric_ranges,omitempty"`
DateRanges []SearchResultDateFacet `json:"date_ranges,omitempty"`
}
// SearchResultStatus holds the status information for an executed search query.
type SearchResultStatus struct {
Total int `json:"total,omitempty"`
Failed int `json:"failed,omitempty"`
Successful int `json:"successful,omitempty"`
}
// SearchResults allows access to the results of a search query.
type SearchResults interface {
Status() SearchResultStatus
Errors() []string
TotalHits() int
Hits() []SearchResultHit
Facets() map[string]SearchResultFacet
Took() time.Duration
MaxScore() float64
}
type searchResponse struct {
Status SearchResultStatus `json:"status,omitempty"`
Errors []string `json:"errors,omitempty"`
TotalHits int `json:"total_hits,omitempty"`
Hits []SearchResultHit `json:"hits,omitempty"`
Facets map[string]SearchResultFacet `json:"facets,omitempty"`
Took uint `json:"took,omitempty"`
MaxScore float64 `json:"max_score,omitempty"`
}
type searchResults struct {
data *searchResponse
}
func (r searchResults) Status() SearchResultStatus {
return r.data.Status
}
func (r searchResults) Errors() []string {
return r.data.Errors
}
func (r searchResults) TotalHits() int {
return r.data.TotalHits
}
func (r searchResults) Hits() []SearchResultHit {
return r.data.Hits
}
func (r searchResults) Facets() map[string]SearchResultFacet {
return r.data.Facets
}
func (r searchResults) Took() time.Duration {
return time.Duration(r.data.Took) / time.Nanosecond
}
func (r searchResults) MaxScore() float64 {
return r.data.MaxScore
}
// Performs a spatial query and returns a list of rows or an error.
func (c *Cluster) doSearchQuery(tracectx opentracing.SpanContext, b *Bucket, q *SearchQuery) (SearchResults, error) {
var err error
var ftsEp string
var timeout time.Duration
var client *http.Client
var creds []UserPassPair
if b != nil {
ftsEp, err = b.getFtsEp()
if err != nil {
return nil, err
}
if b.ftsTimeout < c.ftsTimeout {
timeout = b.ftsTimeout
} else {
timeout = c.ftsTimeout
}
client = b.client.HttpClient()
if c.auth != nil {
creds, err = c.auth.Credentials(AuthCredsRequest{
Service: FtsService,
Endpoint: ftsEp,
Bucket: b.name,
})
if err != nil {
return nil, err
}
} else {
creds = []UserPassPair{
{
Username: b.name,
Password: b.password,
},
}
}
} else {
if c.auth == nil {
panic("Cannot perform cluster level queries without Cluster Authenticator.")
}
tmpB, err := c.randomBucket()
if err != nil {
return nil, err
}
ftsEp, err = tmpB.getFtsEp()
if err != nil {
return nil, err
}
timeout = c.ftsTimeout
client = tmpB.client.HttpClient()
creds, err = c.auth.Credentials(AuthCredsRequest{
Service: FtsService,
Endpoint: ftsEp,
})
if err != nil {
return nil, err
}
}
qIndexName := q.indexName()
qBytes, err := json.Marshal(q.queryData())
if err != nil {
return nil, err
}
var queryData jsonx.DelayedObject
err = json.Unmarshal(qBytes, &queryData)
if err != nil {
return nil, err
}
var ctlData jsonx.DelayedObject
if queryData.Has("ctl") {
err = queryData.Get("ctl", &ctlData)
if err != nil {
return nil, err
}
}
qTimeout := jsonMillisecondDuration(timeout)
if ctlData.Has("timeout") {
err := ctlData.Get("timeout", &qTimeout)
if err != nil {
return nil, err
}
if qTimeout <= 0 || time.Duration(qTimeout) > timeout {
qTimeout = jsonMillisecondDuration(timeout)
}
}
err = ctlData.Set("timeout", qTimeout)
if err != nil {
return nil, err
}
err = queryData.Set("ctl", ctlData)
if err != nil {
return nil, err
}
if len(creds) > 1 {
err = queryData.Set("creds", creds)
if err != nil {
return nil, err
}
}
qBytes, err = json.Marshal(queryData)
if err != nil {
return nil, err
}
reqUri := fmt.Sprintf("%s/api/index/%s/query", ftsEp, qIndexName)
req, err := http.NewRequest("POST", reqUri, bytes.NewBuffer(qBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if len(creds) == 1 {
req.SetBasicAuth(creds[0].Username, creds[0].Password)
}
dtrace := c.agentConfig.Tracer.StartSpan("dispatch",
opentracing.ChildOf(tracectx))
resp, err := doHttpWithTimeout(client, req, timeout)
if err != nil {
dtrace.Finish()
return nil, err
}
dtrace.Finish()
strace := c.agentConfig.Tracer.StartSpan("streaming",
opentracing.ChildOf(tracectx))
ftsResp := searchResponse{}
switch resp.StatusCode {
case 200:
jsonDec := json.NewDecoder(resp.Body)
err = jsonDec.Decode(&ftsResp)
if err != nil {
strace.Finish()
return nil, err
}
case 400:
ftsResp.Status.Total = 1
ftsResp.Status.Failed = 1
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp.Body)
if err != nil {
strace.Finish()
return nil, err
}
ftsResp.Errors = []string{buf.String()}
case 401:
ftsResp.Status.Total = 1
ftsResp.Status.Failed = 1
ftsResp.Errors = []string{"The requested consistency level could not be satisfied before the timeout was reached"}
}
err = resp.Body.Close()
if err != nil {
logDebugf("Failed to close socket (%s)", err)
}
strace.Finish()
if resp.StatusCode != 200 && resp.StatusCode != 400 && resp.StatusCode != 401 {
return nil, &viewError{
Message: "HTTP Error",
Reason: fmt.Sprintf("Status code was %d.", resp.StatusCode),
}
}
return searchResults{
data: &ftsResp,
}, nil
}
// ExecuteSearchQuery performs a n1ql query and returns a list of rows or an error.
func (c *Cluster) ExecuteSearchQuery(q *SearchQuery) (SearchResults, error) {
span := c.agentConfig.Tracer.StartSpan("ExecuteSearchQuery",
opentracing.Tag{Key: "couchbase.service", Value: "fts"})
defer span.Finish()
return c.doSearchQuery(span.Context(), nil, q)
}