-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsearchquery_options.go
122 lines (95 loc) · 3.13 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
package gocb
import (
"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(0)
// SearchScanConsistencyNotBounded indicates no data consistency is required.
SearchScanConsistencyNotBounded = SearchScanConsistency(1)
)
// 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 map[string]interface{}
Timeout time.Duration
RetryStrategy RetryStrategy
parentSpan requestSpanContext
}
func (opts *SearchOptions) toMap() (map[string]interface{}, error) {
data := make(map[string]interface{})
data["size"] = opts.Limit
data["from"] = opts.Skip
data["explain"] = opts.Explain
data["fields"] = opts.Fields
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"] = "not_bounded"
} else {
return nil, makeInvalidArgumentsError("unexpected consistency option")
}
if ctl == nil {
ctl = make(map[string]interface{})
}
ctl["consistency"] = consistency
}
if opts.ConsistentWith != nil {
consistency := make(map[string]interface{})
consistency["level"] = "at_plus"
consistency["vectors"] = opts.ConsistentWith.toSearchMutationState()
if ctl == nil {
ctl = make(map[string]interface{})
}
ctl["consistency"] = consistency
}
if opts.Raw != nil {
for k, v := range opts.Raw {
data[k] = v
}
}
return data, nil
}