-
Notifications
You must be signed in to change notification settings - Fork 104
/
searchquery_options.go
169 lines (128 loc) · 3.97 KB
/
searchquery_options.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
package gocb
import (
"context"
"time"
cbsearch "github.com/couchbase/gocb/v2/search"
)
// SearchHighlightStyle indicates the type of highlighting to use for a search query.
type SearchHighlightStyle string
const (
// DefaultHighlightStyle specifies to use the default to highlight search result hits.
DefaultHighlightStyle SearchHighlightStyle = ""
// HTMLHighlightStyle specifies to use HTML tags to highlight search result hits.
HTMLHighlightStyle SearchHighlightStyle = "html"
// AnsiHightlightStyle specifies to use ANSI tags to highlight search result hits.
AnsiHightlightStyle SearchHighlightStyle = "ansi"
)
// SearchScanConsistency indicates the level of data consistency desired for a search query.
type SearchScanConsistency uint
const (
searchScanConsistencyNotSet SearchScanConsistency = iota
// SearchScanConsistencyNotBounded indicates no data consistency is required.
SearchScanConsistencyNotBounded
)
// SearchHighlightOptions are the options available for search highlighting.
type SearchHighlightOptions struct {
Style SearchHighlightStyle
Fields []string
}
// SearchOptions represents a pending search query.
type SearchOptions struct {
ScanConsistency SearchScanConsistency
Limit uint32
Skip uint32
Explain bool
Highlight *SearchHighlightOptions
Fields []string
Sort []cbsearch.Sort
Facets map[string]cbsearch.Facet
ConsistentWith *MutationState
// Raw provides a way to provide extra parameters in the request body for the query.
Raw map[string]interface{}
Timeout time.Duration
RetryStrategy RetryStrategy
DisableScoring bool
Collections []string
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
// If set to true, will include the SearchRowLocations.
IncludeLocations bool
// Internal: This should never be used and is not supported.
Internal struct {
User string
}
}
func (opts *SearchOptions) toMap(indexName string) (map[string]interface{}, error) {
data := make(map[string]interface{})
if opts.Limit > 0 {
data["size"] = opts.Limit
}
if opts.Skip > 0 {
data["from"] = opts.Skip
}
if opts.Explain {
data["explain"] = opts.Explain
}
if len(opts.Fields) > 0 {
data["fields"] = opts.Fields
}
if len(opts.Sort) > 0 {
data["sort"] = opts.Sort
}
if opts.Highlight != nil {
highlight := make(map[string]interface{})
highlight["style"] = string(opts.Highlight.Style)
highlight["fields"] = opts.Highlight.Fields
data["highlight"] = highlight
}
if opts.Facets != nil {
facets := make(map[string]interface{})
for k, v := range opts.Facets {
facets[k] = v
}
data["facets"] = facets
}
if opts.ScanConsistency != 0 && opts.ConsistentWith != nil {
return nil, makeInvalidArgumentsError("ScanConsistency and ConsistentWith must be used exclusively")
}
var ctl map[string]interface{}
if opts.ScanConsistency != searchScanConsistencyNotSet {
consistency := make(map[string]interface{})
if opts.ScanConsistency == SearchScanConsistencyNotBounded {
consistency["level"] = ""
} else {
return nil, makeInvalidArgumentsError("unexpected consistency option")
}
ctl = map[string]interface{}{"consistency": consistency}
}
if opts.ConsistentWith != nil {
consistency := make(map[string]interface{})
consistency["level"] = "at_plus"
consistency["vectors"] = opts.ConsistentWith.toSearchMutationState(indexName)
if ctl == nil {
ctl = make(map[string]interface{})
}
ctl["consistency"] = consistency
}
if ctl != nil {
data["ctl"] = ctl
}
if opts.DisableScoring {
data["score"] = "none"
}
if opts.Raw != nil {
for k, v := range opts.Raw {
data[k] = v
}
}
if len(opts.Collections) > 0 {
data["collections"] = opts.Collections
}
if opts.IncludeLocations {
data["includeLocations"] = true
}
return data, nil
}