-
Notifications
You must be signed in to change notification settings - Fork 0
/
esclient.go
62 lines (52 loc) · 1.31 KB
/
esclient.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
package elastiquery
import (
"context"
"encoding/json"
)
// ESClient is a version-independent ElasticSearch client.
type ESClient interface {
RawQuery(string) ESQuery
PrefixQuery(string, string) ESQuery
TermQuery(string, string) ESQuery
AndQuery(...ESQuery) ESQuery
OrQuery(...ESQuery) ESQuery
RangeQuery(string, interface{}, interface{}) ESQuery
}
// ESQuery is a version-independent ElasticSearch query.
type ESQuery interface {
Do(ctx context.Context, index string, opts ...QueryOpt) (ESResult, error)
}
// ESResult is a version-independent ElasticSearch query result.
type ESResult interface {
TotalHits() int64
RawHits() []*json.RawMessage
}
type QueryOpt func(*QueryParams)
func WithOffset(offset int) QueryOpt {
return func(qp *QueryParams) {
qp.Offset = offset
}
}
func WithLimit(limit int) QueryOpt {
return func(qp *QueryParams) {
qp.Limit = limit
}
}
func WithSortField(field string) QueryOpt {
return func(qp *QueryParams) {
qp.SortField = field
}
}
func WithReverseSort() QueryOpt {
return func(qp *QueryParams) {
qp.SortReverse = true
}
}
// QueryParams are common parameters for queries. It should not normally be used
// directly - use the `With...` functions to modify the defaults instead.
type QueryParams struct {
Offset int
Limit int
SortField string
SortReverse bool
}