forked from couchbase/gocb
/
searchquery.go
161 lines (139 loc) · 4.83 KB
/
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
package gocb
import (
"time"
)
// 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")
)
type searchQueryHighlightData struct {
Style string `json:"style,omitempty"`
Fields []string `json:"fields,omitempty"`
}
type searchQueryConsistencyData struct {
Level string `json:"level,omitempty"`
Vectors *MutationState `json:"vectors,omitempty"`
}
type searchQueryCtlData struct {
Timeout uint `json:"timeout,omitempty"`
Consistency *searchQueryConsistencyData `json:"consistency,omitempty"`
}
type searchQueryData struct {
Query interface{} `json:"query,omitempty"`
Size int `json:"size,omitempty"`
From int `json:"from,omitempty"`
Explain bool `json:"explain,omitempty"`
Highlight *searchQueryHighlightData `json:"highlight,omitempty"`
Fields []string `json:"fields,omitempty"`
Sort []interface{} `json:"sort,omitempty"`
Facets map[string]interface{} `json:"facets,omitempty"`
Ctl *searchQueryCtlData `json:"ctl,omitempty"`
}
// SearchQuery represents a pending search query.
type SearchQuery struct {
name string
data searchQueryData
}
// Limit specifies a limit on the number of results to return.
func (sq *SearchQuery) Limit(value int) *SearchQuery {
sq.data.Size = value
return sq
}
// Skip specifies how many results to skip at the beginning of the result list.
func (sq *SearchQuery) Skip(value int) *SearchQuery {
sq.data.From = value
return sq
}
// Explain enables search query explanation which provides details on how a query is executed.
func (sq *SearchQuery) Explain(value bool) *SearchQuery {
sq.data.Explain = value
return sq
}
// Highlight specifies how to highlight the hits in the search result.
func (sq *SearchQuery) Highlight(style SearchHighlightStyle, fields ...string) *SearchQuery {
if sq.data.Highlight == nil {
sq.data.Highlight = &searchQueryHighlightData{}
}
sq.data.Highlight.Style = string(style)
sq.data.Highlight.Fields = fields
return sq
}
// Fields specifies which fields you wish to return in the results.
func (sq *SearchQuery) Fields(fields ...string) *SearchQuery {
sq.data.Fields = fields
return sq
}
// Sort specifies a sorting order for the results. Only available in Couchbase Server 4.6+.
func (sq *SearchQuery) Sort(fields ...interface{}) *SearchQuery {
sq.data.Sort = fields
return sq
}
// AddFacet adds a new search facet to include in the results.
func (sq *SearchQuery) AddFacet(name string, facet interface{}) *SearchQuery {
if sq.data.Facets == nil {
sq.data.Facets = make(map[string]interface{})
}
sq.data.Facets[name] = facet
return sq
}
// Timeout indicates the maximum time to wait for this query to complete.
func (sq *SearchQuery) Timeout(value time.Duration) *SearchQuery {
if sq.data.Ctl == nil {
sq.data.Ctl = &searchQueryCtlData{}
}
sq.data.Ctl.Timeout = uint(value / time.Millisecond)
return sq
}
// Consistency specifies the level of consistency required for this query.
func (sq *SearchQuery) Consistency(stale ConsistencyMode) *SearchQuery {
if sq.data.Ctl == nil {
sq.data.Ctl = &searchQueryCtlData{}
}
if sq.data.Ctl.Consistency == nil {
sq.data.Ctl.Consistency = &searchQueryConsistencyData{}
}
if sq.data.Ctl.Consistency.Vectors != nil {
panic("Consistent and ConsistentWith must be used exclusively")
}
if stale == NotBounded {
sq.data.Ctl.Consistency.Level = "not_bounded"
} else {
panic("Unexpected consistency option")
}
return sq
}
// ConsistentWith specifies a mutation state to be consistent with for this query.
func (sq *SearchQuery) ConsistentWith(state *MutationState) *SearchQuery {
if sq.data.Ctl == nil {
sq.data.Ctl = &searchQueryCtlData{}
}
if sq.data.Ctl.Consistency == nil {
sq.data.Ctl.Consistency = &searchQueryConsistencyData{}
}
if sq.data.Ctl.Consistency.Level != "" {
panic("Consistent and ConsistentWith must be used exclusively")
}
sq.data.Ctl.Consistency.Level = "at_plus"
sq.data.Ctl.Consistency.Vectors = state
return sq
}
func (sq *SearchQuery) indexName() string {
return sq.name
}
func (sq *SearchQuery) queryData() interface{} {
return sq.data
}
// NewSearchQuery creates a new SearchQuery object from an index name and query.
func NewSearchQuery(indexName string, query interface{}) *SearchQuery {
q := &SearchQuery{
name: indexName,
}
q.data.Query = query
return q
}