-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.go
More file actions
91 lines (72 loc) · 1.45 KB
/
query.go
File metadata and controls
91 lines (72 loc) · 1.45 KB
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
package es
import (
"encoding/json"
"fmt"
)
type QueryHead struct {
Query Query
}
func (qh *QueryHead) MarshalJSON() ([]byte, error) {
if nil == qh.Query {
return nil, fmt.Errorf("empty query")
}
return json.Marshal(map[string]interface{}{
qh.Query.Name(): qh.Query,
})
}
type Query interface {
json.Marshaler
Name() string
}
type TextValue string
func (q *TextValue) MarshalJSON() ([]byte, error) {
return []byte(*q), nil
}
const (
MATCH_OPERATER_OR = iota // default
MATCH_OPERATER_AND
)
const (
MATCH_TYPE_BOOL = iota // default
MATCH_TYPE_PHRASE
MATCH_TYPE_PHRASE_PREFIX
)
type MatchAllQuery struct{}
func (q *MatchAllQuery) Name() string {
return "match_all"
}
func (q *MatchAllQuery) MarshalJSON() ([]byte, error) {
return []byte("{}"), nil
}
type MatchQuery struct {
Field string
Query string
Type int
Operator int
}
func (q *MatchQuery) Name() string {
return "match"
}
func (q *MatchQuery) MarshalJSON() ([]byte, error) {
if q.Field == "" {
return nil, fmt.Errorf("empty field")
}
format := struct {
Query string `json:"query"`
Type string `json:"type,omitempty"`
Operator string `json:"operator"`
}{
Query: q.Query,
}
if q.Operator == MATCH_OPERATER_OR {
format.Operator = "or"
} else {
format.Operator = "and"
}
if q.Type == MATCH_TYPE_PHRASE {
format.Type = "phrase"
} else if q.Type == MATCH_TYPE_PHRASE_PREFIX {
format.Type = "phrase_prefix"
}
return json.Marshal(&format)
}