-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
576 lines (514 loc) · 17.6 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package solr
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
// Query Options and other constants
const (
OptionDebug = "debug"
OptionDefType = "defType"
OptionQ = "q"
OptionQOperation = "q.op"
OptionFilter = "fq"
OptionFieldList = "fl"
OptionRows = "rows"
OptionStart = "start"
OptionSort = "sort"
OptionWT = "wt"
OptionCommit = "commit"
OptionOverwrite = "overwrite"
OptionCommitWithin = "commitWithin"
OptionWaitSearcher = "waitSearcher"
OptionMaxSegments = "maxSegments"
OptionExpungeDeletes = "expungeDeletes"
OptionMM = "mm"
OptionBoost = "boost"
OptionQueryFields = "qf"
OptionBoostQuery = "bq"
OptionBoostFunctions = "bf"
OptionUserFields = "uf"
OptionCollapseField = "field"
OptionCollapseMax = "max"
OptionCollapseMin = "min"
OptionCollapseSort = "sort"
OptionCollapseNullPolicy = "nullPolicy"
OptionCollapseHint = "hint"
OptionCollapseSize = "size"
OptionExpand = "expand"
OptionExpandSort = "expand.sort"
OptionExpandQ = "expand.q"
OptionExpandFQ = "expand.fq"
OptionExpandRows = "expand.rows"
OptionFacet = "facet"
OptionFacetField = "facet.field"
OptionLimit = "limit"
OptionPrefix = "prefix"
OptionContains = "contains"
OptionMissing = "missing"
OptionMinCount = "mincount"
OptionExcludeTerms = "excludeTerms"
OptionFacetPivot = "facet.pivot"
OptionGroup = "group"
OptionGroupField = "group.field"
OptionGroupNGroups = "group.ngroups"
OptionGroupLimit = "group.limit"
OptionGroupOffset = "group.offset"
OptionGroupQuery = "group.query"
OptionGroupFunc = "group.func"
OptionGroupSort = "group.sort"
ReturnTypeJSON = "json"
QOperationOR = "OR"
QOperationAND = "AND"
DefTypeDisMax DefType = "dismax"
DefTypeEDisMax DefType = "edismax"
DefTypeStandard DefType = "lucene"
DebugTypeQuery DebugType = "query"
DebugTypeTiming DebugType = "timing"
DebugTypeResults DebugType = "results"
DebugTypeAll DebugType = "all"
)
// DebugType is used to restrict the available debug types for a
// `/search` request
type DebugType string
func (dt DebugType) String() string {
return string(dt)
}
func (dt DebugType) isValid() bool {
return !(dt != DebugTypeQuery && dt != DebugTypeTiming && dt != DebugTypeResults && dt != DebugTypeAll)
}
// DefType is used to restrict the available defTypes for a
// `/search` request
type DefType string
func (dt DefType) String() string {
return string(dt)
}
func (dt DefType) isValid() bool {
return !(dt != DefTypeDisMax && dt != DefTypeEDisMax && dt != DefTypeStandard)
}
// Returned validation errors
var (
ErrInvalidDefType = errors.New("invalid defType, please use one of the provided ones")
ErrInvalidDebugType = errors.New("invalid debugType, please use one of the provided ones")
)
// WriteOptions contains options for write actions. Those include:
// Commit: Autocommit all changes alongside the current request
// CommitWithin: Autocommit all changes after the specified
// time (in miliseconds)
// AllowDuplicate: Allows uniqueKey duplication
type WriteOptions struct {
Commit bool
CommitWithin int64
AllowDuplicate bool
}
func (opts *WriteOptions) formatQueryFromOpts() url.Values {
if opts == nil {
return nil
}
q := make(url.Values)
if opts.Commit {
q.Set(OptionCommit, "true")
}
if opts.CommitWithin > 0 {
q.Set(OptionCommitWithin, strconv.FormatInt(opts.CommitWithin, 10))
}
if opts.AllowDuplicate {
q.Set(OptionOverwrite, "false")
}
return q
}
// ReadOptions contains options for read actions. Those include:
// Debug: Sets the type of debugging for the request
// DefType: Sets the type of query parse to use (default: lucene)
// Rows: Sets the number of rows to return
type ReadOptions struct {
Debug DebugType
DefType DefType
Rows int
}
// Query represents the query parameters of a search. It provides
// helper methods for most of the available solr query params.
type Query struct {
q []string
qOp string
params url.Values
}
// NewQuery returns an initialized Query. It accepts as options a result
// rows limit and a debug type. It sets by default the return type
// to JSON, as it is the only type supported by this library.
func NewQuery(opts *ReadOptions) *Query {
nq := &Query{qOp: QOperationOR}
nq.params = make(url.Values)
if opts != nil {
if opts.Debug != "" && opts.Debug.isValid() {
nq.params.Set(OptionDebug, opts.Debug.String())
}
if opts.DefType != "" && opts.DefType.isValid() {
nq.params.Set(OptionDefType, opts.DefType.String())
}
if opts.Rows > 0 {
sv := strconv.Itoa(opts.Rows)
nq.params.Set(OptionRows, sv)
}
}
return nq
}
// AddParam allows the addition of custom query parameters.
func (q *Query) AddParam(key, value string) {
q.params.Add(key, value)
}
// SetParam allows the setting of custom query parameters.
func (q *Query) SetParam(key, value string) {
q.params.Set(key, value)
}
// DelParam allows the deletion of query parameters.
func (q *Query) DelParam(key string) {
q.params.Del(key)
}
// AddQuery adds a key-value pair to the Q parameter and facilitates
// the formulation of simple boolean queries. The field can be an
// empty string in the case of text search or an existing qf
// parameter. Using this will overwrite any call to the
// `SetQuery` method. For complex logic use that instead
func (q *Query) AddQuery(field, value string) {
if field == "" {
q.q = append(q.q, value)
} else {
q.q = append(q.q, fmt.Sprintf("%s:%s", field, value))
}
}
// SetQuery sets the Q parameter of the query.
func (q *Query) SetQuery(value string) {
q.params.Set(OptionQ, value)
}
// DelQuery removes any Q parameters that have been added
func (q *Query) DelQuery() {
q.params.Del(OptionQ)
q.q = []string{}
}
// SetOperationAND sets the operation for the Q parameter
// to AND (only when using `AddQuery`)
func (q *Query) SetOperationAND() {
q.qOp = QOperationAND
}
// SetOperationOR sets the operation for the Q parameter
// to OR (only when using `AddQuery`)
func (q *Query) SetOperationOR() {
q.qOp = QOperationOR
}
// AddFilter adds a key-value pair on which to filter the query.
// More info:
// https://lucene.apache.org/solr/guide/8_5/common-query-parameters.html#fq-filter-query-parameter
func (q *Query) AddFilter(key, value string) {
q.params.Add(OptionFilter, fmt.Sprintf("%s:%s", key, value))
}
// SetFilter gives the option to set a filter allowing for more complex logic instead
// of a basic key-value check.
func (q *Query) SetFilter(value string) {
q.params.Set(OptionFilter, value)
}
// AddField adds the given field to the returned field list.
// More info:
// https://lucene.apache.org/solr/guide/8_5/common-query-parameters.html#fl-field-list-parameter
func (q *Query) AddField(value string) {
q.params.Add(OptionFieldList, value)
}
// SetStart enables setting the starting index for a search query. It can be used when
// the available results are more than the rows returned to fetch the remainder rows.
// More info:
// https://lucene.apache.org/solr/guide/8_5/common-query-parameters.html#start-parameter
func (q *Query) SetStart(value int) {
sv := strconv.Itoa(value)
q.params.Set(OptionStart, sv)
}
// SetSort sets the way the results are sorted. It should be formatted using the following
// protocol "<field name> <direction>, <field name> <direction>,..."
// More info:
// https://lucene.apache.org/solr/guide/8_5/common-query-parameters.html#sort-parameter
func (q *Query) SetSort(value string) {
q.params.Set(OptionSort, value)
}
// SetRows sets the amount of rows to be returned from the query overwritting the
// default value lucene.apache.org/solr/guide/8_5/common-query-parameters.html#rows-parameter
func (q *Query) SetRows(value int) {
sv := strconv.Itoa(value)
q.params.Set(OptionRows, sv)
}
// String returns the string representation of the query.
func (q *Query) String() string {
if len(q.q) > 0 {
q.params.Set(OptionQ, strings.Join(q.q, fmt.Sprintf(" %s ", q.qOp)))
}
q.params.Set(OptionWT, ReturnTypeJSON)
return q.params.Encode()
}
// CollapseParams are the available params that can be set when using
// the Collapsing Query Parser
type CollapseParams struct {
Field string
Min string
Max string
Sort string
NullPolicy *NullPolicy
Hint *Hint
Size string
}
// NullPolicy determines the policy when the collapsing field
// value is null on the document
type NullPolicy string
func (p NullPolicy) String() string {
return string(p)
}
// Hint represents the Collapse hint param
type Hint string
func (h Hint) String() string {
return string(h)
}
// Constants to secure proper NullPolicy & Hint usage
const (
NullPolicyIgnore NullPolicy = "ignore"
NullPolicyExpand NullPolicy = "expand"
NullPolicyCollapse NullPolicy = "collapse"
HintTopFC Hint = "top_fc"
)
// Possible errors returned from improper use of the Collapsing Query Parser
var (
ErrParamsRequired = errors.New("param field is required for the CollapsingQParser")
ErrTooManyParams = errors.New("only one of Max, Min or Sort may be populated")
ErrInvalidNullPolicy = errors.New("invalid null policy, please use one of the provided")
ErrInvalidHint = errors.New("invalid hint, please use one of the provided")
)
func paramFormat(k, v string) string {
return fmt.Sprintf("%s=%s", k, v)
}
func (p *CollapseParams) format() (string, error) {
if p == nil {
return "", ErrParamsRequired
}
if p.Field == "" {
return "", ErrParamsRequired
}
params := []string{paramFormat(OptionCollapseField, p.Field)}
var c int
if p.Max != "" {
params = append(params, paramFormat(OptionCollapseMax, p.Max))
c++
}
if p.Min != "" {
params = append(params, paramFormat(OptionCollapseMin, p.Min))
c++
}
if p.Sort != "" {
params = append(params, paramFormat(OptionCollapseSort, p.Sort))
c++
}
if c > 1 {
return "", ErrTooManyParams
}
if p.NullPolicy != nil {
if *p.NullPolicy != NullPolicyIgnore && *p.NullPolicy != NullPolicyCollapse && *p.NullPolicy != NullPolicyExpand {
return "", ErrInvalidNullPolicy
}
params = append(params, paramFormat(OptionCollapseNullPolicy, p.NullPolicy.String()))
}
if p.Hint != nil {
if *p.Hint != HintTopFC {
return "", ErrInvalidHint
}
params = append(params, paramFormat(OptionCollapseHint, p.Hint.String()))
}
if p.Size != "" {
params = append(params, paramFormat(OptionCollapseSize, p.Size))
}
return fmt.Sprintf("{!collapse %s}", strings.Join(params, " ")), nil
}
// Collapse sets the Collapsing Query Parser post filter that groups
// documents according to the given parameters.
// More Info:
// https://lucene.apache.org/solr/guide/8_5/collapse-and-expand-results.html#collapsing-query-parser
func (q *Query) Collapse(params *CollapseParams) error {
collapseString, err := params.format()
if err != nil {
return err
}
q.params.Add(OptionFilter, collapseString)
return nil
}
// ExpandOptions are the available options to set for the expand component
type ExpandOptions struct {
Sort string
Rows int
Q string
FQ string
}
// Expand sets the parameter than returns an expand component used to expand the groups
// that were collapsed by the Collapsing Query Parser. The optional params override
// the original query values
// More info:
// https://lucene.apache.org/solr/guide/8_5/collapse-and-expand-results.html#expand-component
func (q *Query) Expand(opts *ExpandOptions) {
q.params.Add(OptionExpand, "true")
if opts != nil {
if opts.Sort != "" {
q.params.Add(OptionExpandSort, opts.Sort)
}
if opts.Q != "" {
q.params.Add(OptionExpandQ, opts.Q)
}
if opts.FQ != "" {
q.params.Add(OptionExpandFQ, opts.FQ)
}
if opts.Rows > 0 {
rv := strconv.Itoa(opts.Rows)
q.params.Add(OptionExpandRows, rv)
}
}
}
// SetQueryFields sets the fields to search (DisMax & eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-dismax-query-parser.html#qf-query-fields-parameter
func (q *Query) SetQueryFields(fields []string) {
fieldsStr := strings.Join(fields, " ")
q.params.Set(OptionQueryFields, fieldsStr)
}
// SetMinimumShouldMatch sets the minimum params to match (DisMax & eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-dismax-query-parser.html#mm-minimum-should-match-parameter
func (q *Query) SetMinimumShouldMatch(value string) {
q.params.Set(OptionMM, value)
}
// SetBoostFunctions sets the boost functions param (DisMax & eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-dismax-query-parser.html#bf-boost-functions-parameter
func (q *Query) SetBoostFunctions(value string) {
q.params.Set(OptionBoostFunctions, value)
}
// SetBoostQuery sets the boost query param (DisMax & eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-dismax-query-parser.html#bq-boost-query-parameter
func (q *Query) SetBoostQuery(value string) {
q.params.Set(OptionBoostQuery, value)
}
// SetBoost sets the boost param (eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-extended-dismax-query-parser.html#extended-dismax-parameters
func (q *Query) SetBoost(value string) {
q.params.Set(OptionBoost, value)
}
// SetUserFields sets the fields a user is allowed to query (eDisMax only)
// More info:
// https://lucene.apache.org/solr/guide/8_5/the-extended-dismax-query-parser.html#extended-dismax-parameters
func (q *Query) SetUserFields(fields []string) {
fieldsStr := strings.Join(fields, " ")
q.params.Set(OptionUserFields, fieldsStr)
}
// Facet represent a facet for a specific field along with
// some of the available options for that facet.
type Facet struct {
Field string
Prefix string
Contains string
Limit int
MinCount int
Missing bool
ExcludeTerms []string
}
func (f *Facet) format(param string) string {
return fmt.Sprintf("f.%s.facet.%s", f.Field, param)
}
// AddFacet adds a facet to the query, along with field specific options.
// Not all options are supported, but functions like AddParam, SetParam
// can help with those missing options.
// More info:
// https://lucene.apache.org/solr/guide/8_5/faceting.html
func (q *Query) AddFacet(f *Facet) {
q.params.Set(OptionFacet, "true")
q.params.Add(OptionFacetField, f.Field)
if f.MinCount > 0 {
minCount := strconv.Itoa(f.MinCount)
q.params.Set(f.format(OptionMinCount), minCount)
}
if f.Limit != 0 {
limit := strconv.Itoa(f.Limit)
q.params.Set(f.format(OptionLimit), limit)
}
if f.Prefix != "" {
q.params.Set(f.format(OptionPrefix), f.Prefix)
}
if f.Contains != "" {
q.params.Set(f.format(OptionContains), f.Contains)
}
if f.Missing {
q.params.Set(f.format(OptionMissing), "true")
}
if len(f.ExcludeTerms) > 1 {
q.params.Set(f.format(OptionExcludeTerms), "true")
}
}
// AddFacetPivot adds a facet pivot. The given fieldsString should contain the fields
// to be faceted separated with a comma. The minCount parameter defines the minimum
// number of documents that need to match in order for the facet to be included
// in the results. The default is 1.
// More info:
// https://lucene.apache.org/solr/guide/8_5/faceting.html#pivot-decision-tree-faceting
func (q *Query) AddFacetPivot(fieldsString string, minCount int) {
q.params.Set(OptionFacet, "true")
q.params.Add(OptionFacetPivot, fieldsString)
if minCount > 1 {
minCountStr := strconv.Itoa(minCount)
q.params.Set(fmt.Sprintf("%s.%s", OptionFacetPivot, OptionMinCount), minCountStr)
}
}
// GroupParams contains the available parameters to finetune result
// grouping. Of all the params only Field is required
type GroupParams struct {
Field string
Func []string
Query []string
Limit int
Offset int
Sort string
ShowGroupsNumber bool
}
// Group sets the grouping parameters for a query to facilitate result
// grouping. The GroupParams must be present with at least the field
// parameter filled.
// More info:
// https://lucene.apache.org/solr/guide/8_5/result-grouping.html
func (q *Query) Group(params *GroupParams) error {
if params == nil {
return ErrParamsRequired
}
if params.Field == "" && len(params.Query) == 0 && len(params.Func) == 0 {
return ErrParamsRequired
}
q.params.Set(OptionGroup, "true")
if params.Field != "" {
q.params.Set(OptionGroupField, params.Field)
}
if params.ShowGroupsNumber {
q.params.Set(OptionGroupNGroups, "true")
}
if params.Limit > 1 {
q.params.Set(OptionGroupLimit, strconv.Itoa(params.Limit))
}
if params.Offset > 0 {
q.params.Set(OptionGroupOffset, strconv.Itoa(params.Offset))
}
if params.Sort != "" {
q.params.Set(OptionGroupSort, params.Sort)
}
if len(params.Query) > 0 {
for _, i := range params.Query {
q.params.Add(OptionGroupQuery, i)
}
}
if len(params.Func) > 0 {
for _, i := range params.Func {
q.params.Add(OptionGroupFunc, i)
}
}
return nil
}