-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_searchquery.go
331 lines (276 loc) · 8.45 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package gocb
import (
"encoding/json"
"time"
cbsearch "github.com/couchbase/gocb/v2/search"
gocbcore "github.com/couchbase/gocbcore/v8"
)
type jsonRowLocation struct {
Field string `json:"field"`
Term string `json:"term"`
Position uint32 `json:"position"`
Start uint32 `json:"start"`
End uint32 `json:"end"`
ArrayPositions []uint32 `json:"array_positions"`
}
type jsonSearchFacet struct {
Name string `json:"name"`
Field string `json:"field"`
Total uint64 `json:"total"`
Missing uint64 `json:"missing"`
Other uint64 `json:"other"`
}
type jsonSearchRowLocations map[string]map[string][]jsonRowLocation
type jsonSearchRow struct {
Index string `json:"index"`
ID string `json:"id"`
Score float64 `json:"score"`
Explanation interface{} `json:"explanation"`
Locations jsonSearchRowLocations `json:"locations"`
Fragments map[string][]string `json:"fragments"`
Fields json.RawMessage `json:"fields"`
}
type jsonSearchResponse struct {
Errors map[string]string `json:"errors"`
TotalHits uint64 `json:"total_hits"`
MaxScore float64 `json:"max_score"`
Took uint64 `json:"took"`
Facets map[string]jsonSearchFacet `json:"facets"`
}
// SearchMetrics encapsulates various metrics gathered during a search queries execution.
type SearchMetrics struct {
Took time.Duration
TotalRows uint64
MaxScore float64
TotalPartitionCount uint64
SuccessPartitionCount uint64
ErrorPartitionCount uint64
}
func (metrics *SearchMetrics) fromData(data jsonSearchResponse) error {
metrics.TotalRows = data.TotalHits
metrics.MaxScore = data.MaxScore
metrics.Took = time.Duration(data.Took) * time.Microsecond
return nil
}
// SearchMetaData provides access to the meta-data properties of a search query result.
type SearchMetaData struct {
Metrics SearchMetrics
Errors map[string]string
}
func (meta *SearchMetaData) fromData(data jsonSearchResponse) error {
metrics := SearchMetrics{}
if err := metrics.fromData(data); err != nil {
return err
}
meta.Metrics = metrics
meta.Errors = data.Errors
return nil
}
// SearchFacetResult provides access to the result of a faceted query.
type SearchFacetResult struct {
Name string
Field string
Total uint64
Missing uint64
Other uint64
}
func (fr *SearchFacetResult) fromData(data jsonSearchFacet) error {
fr.Name = data.Name
fr.Field = data.Field
fr.Total = data.Total
fr.Missing = data.Missing
fr.Other = data.Other
return nil
}
// SearchRowLocation represents the location of a row match
type SearchRowLocation struct {
Position uint32
Start uint32
End uint32
ArrayPositions []uint32
}
func (rl *SearchRowLocation) fromData(data jsonRowLocation) error {
rl.Position = data.Position
rl.Start = data.Start
rl.End = data.End
rl.ArrayPositions = data.ArrayPositions
return nil
}
// SearchRow represents a single hit returned from a search query.
type SearchRow struct {
Index string
ID string
Score float64
Explanation interface{}
Locations map[string]map[string][]SearchRowLocation
Fragments map[string][]string
fieldsBytes []byte
}
// Fields decodes the fields included in a search hit.
func (sr *SearchRow) Fields(valuePtr interface{}) error {
return json.Unmarshal(sr.fieldsBytes, valuePtr)
}
// SearchResult allows access to the results of a search query.
type SearchResult struct {
reader *gocbcore.SearchRowReader
currentRow SearchRow
}
func newSearchResult(reader *gocbcore.SearchRowReader) (*SearchResult, error) {
return &SearchResult{
reader: reader,
}, nil
}
// Next assigns the next result from the results into the value pointer, returning whether the read was successful.
func (r *SearchResult) Next() bool {
rowBytes := r.reader.NextRow()
if rowBytes == nil {
return false
}
r.currentRow = SearchRow{}
var rowData jsonSearchRow
if err := json.Unmarshal(rowBytes, &rowData); err == nil {
r.currentRow.Index = rowData.Index
r.currentRow.ID = rowData.ID
r.currentRow.Score = rowData.Score
r.currentRow.Explanation = rowData.Explanation
r.currentRow.Fragments = rowData.Fragments
r.currentRow.fieldsBytes = rowData.Fields
locations := make(map[string]map[string][]SearchRowLocation)
for fieldName, fieldData := range rowData.Locations {
terms := make(map[string][]SearchRowLocation)
for termName, termData := range fieldData {
locations := make([]SearchRowLocation, len(termData))
for locIdx, locData := range termData {
locations[locIdx].fromData(locData)
}
terms[termName] = locations
}
locations[fieldName] = terms
}
r.currentRow.Locations = locations
}
return true
}
// Row returns the contents of the current row.
func (r *SearchResult) Row() SearchRow {
return r.currentRow
}
// Err returns any errors that have occurred on the stream
func (r *SearchResult) Err() error {
return r.reader.Err()
}
// Close marks the results as closed, returning any errors that occurred during reading the results.
func (r *SearchResult) Close() error {
return r.reader.Close()
}
func (r *SearchResult) getJSONResp() (jsonSearchResponse, error) {
metaDataBytes, err := r.reader.MetaData()
if err != nil {
return jsonSearchResponse{}, err
}
var jsonResp jsonSearchResponse
err = json.Unmarshal(metaDataBytes, &jsonResp)
if err != nil {
return jsonSearchResponse{}, err
}
return jsonResp, nil
}
// MetaData returns any meta-data that was available from this query. Note that
// the meta-data will only be available once the object has been closed (either
// implicitly or explicitly).
func (r *SearchResult) MetaData() (*SearchMetaData, error) {
jsonResp, err := r.getJSONResp()
if err != nil {
return nil, err
}
var metaData SearchMetaData
err = metaData.fromData(jsonResp)
if err != nil {
return nil, err
}
return &metaData, nil
}
// Facets returns any facets that were returned with this query. Note that the
// facets will only be available once the object has been closed (either
// implicitly or explicitly).
func (r *SearchResult) Facets() (map[string]SearchFacetResult, error) {
jsonResp, err := r.getJSONResp()
if err != nil {
return nil, err
}
facets := make(map[string]SearchFacetResult)
for facetName, facetData := range jsonResp.Facets {
var facet SearchFacetResult
err := facet.fromData(facetData)
if err != nil {
return nil, err
}
facets[facetName] = facet
}
return facets, nil
}
// SearchQuery executes the analytics query statement on the server.
func (c *Cluster) SearchQuery(indexName string, query cbsearch.Query, opts *SearchOptions) (*SearchResult, error) {
if opts == nil {
opts = &SearchOptions{}
}
span := c.sb.Tracer.StartSpan("SearchQuery", opts.parentSpan).
SetTag("couchbase.service", "search")
defer span.Finish()
timeout := c.sb.QueryTimeout
if opts.Timeout != 0 && opts.Timeout < timeout {
timeout = opts.Timeout
}
deadline := time.Now().Add(timeout)
retryStrategy := c.sb.RetryStrategyWrapper
if opts.RetryStrategy != nil {
retryStrategy = newRetryStrategyWrapper(opts.RetryStrategy)
}
searchOpts, err := opts.toMap()
if err != nil {
return nil, SearchError{
InnerError: wrapError(err, "failed to generate query options"),
Query: query,
}
}
searchOpts["query"] = query
return c.execSearchQuery(span, indexName, searchOpts, deadline, retryStrategy)
}
func maybeGetSearchOptionQuery(options map[string]interface{}) interface{} {
if value, ok := options["query"]; ok {
return value
}
return ""
}
func (c *Cluster) execSearchQuery(
span requestSpan,
indexName string,
options map[string]interface{},
deadline time.Time,
retryStrategy *retryStrategyWrapper,
) (*SearchResult, error) {
provider, err := c.getSearchProvider()
if err != nil {
return nil, SearchError{
InnerError: wrapError(err, "failed to get query provider"),
Query: maybeGetSearchOptionQuery(options),
}
}
reqBytes, err := json.Marshal(options)
if err != nil {
return nil, SearchError{
InnerError: wrapError(err, "failed to marshall query body"),
Query: maybeGetSearchOptionQuery(options),
}
}
res, err := provider.SearchQuery(gocbcore.SearchQueryOptions{
IndexName: indexName,
Payload: reqBytes,
RetryStrategy: retryStrategy,
Deadline: deadline,
})
if err != nil {
return nil, maybeEnhanceSearchError(err)
}
return newSearchResult(res)
}