-
Notifications
You must be signed in to change notification settings - Fork 11
/
query.go
123 lines (99 loc) · 2.34 KB
/
query.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
package solr
// Query is a query
type Query struct {
// common query params
// Refer to https://lucene.apache.org/solr/guide/8_7/common-query-parameters.html
// standard params in v1 api
// debug string
// explainOther string
// timeAllowed int
// segmentTerminateEarly bool
// omitHeader bool
// echoParams string
// minExactCount int
// responseWriter string // wt
// supported params in json request api
sort string
offset int // start
limit int // rows
filters []string // fq
fields []string // fl
// query is the main query
query string
// facets
// Refer to https://lucene.apache.org/solr/guide/8_7/json-facet-api.html
facets []Faceter
// additional queries
// https://lucene.apache.org/solr/guide/8_7/json-query-dsl.html#additional-queries
queries M
}
// NewQuery accepts the main query built from the various
// query parsers and returns the Query object.
func NewQuery(query string) *Query {
return &Query{query: query}
}
// BuildQuery builds the query
func (q *Query) BuildQuery() M {
qm := M{"query": q.query}
if q.queries != nil {
qm["queries"] = q.queries
}
if q.sort != "" {
qm["sort"] = q.sort
}
if q.offset != 0 {
qm["offset"] = q.offset
}
if q.limit != 0 {
qm["limit"] = q.limit
}
if len(q.filters) > 0 {
qm["filter"] = q.filters
}
if len(q.fields) != 0 {
qm["fields"] = q.fields
}
if len(q.facets) > 0 {
facets := M{}
for _, facet := range q.facets {
facets[facet.Name()] = facet.BuildFacet()
}
qm["facet"] = facets
}
return qm
}
// Sort sets the sort param
func (q *Query) Sort(sort string) *Query {
q.sort = sort
return q
}
// Offset sets the offset param
func (q *Query) Offset(offset int) *Query {
q.offset = offset
return q
}
// Limit sets the limit param
func (q *Query) Limit(limit int) *Query {
q.limit = limit
return q
}
// Filters sets the filter param
func (q *Query) Filters(filters ...string) *Query {
q.filters = filters
return q
}
// Fields sets the fields param
func (q *Query) Fields(fields ...string) *Query {
q.fields = fields
return q
}
// Facets sets the facet query
func (q *Query) Facets(facets ...Faceter) *Query {
q.facets = facets
return q
}
// Queries sets the additional queries
func (q *Query) Queries(queries M) *Query {
q.queries = queries
return q
}