-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
65 lines (54 loc) · 1.41 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
package queryparser
import (
"fmt"
"strings"
)
// Query represents a query with one or more terms, how to decide matching, and zero or more subqueries
type Query struct {
Terms []string
Occur Occur
SubQueries []Query
}
// Term represents a word or phrase
type Term string
// Occur represents whether we want documents containing all terms and matching all subqueries (MUST) or at least one term or matching at least one subquery (SHOULD)
type Occur int
const (
// MUST means we want documents containing all terms and matching all subqueries
MUST Occur = iota
// SHOULD means we want documents containing at least one term or matching at least one subquery
SHOULD
)
func (occur Occur) string() string {
switch occur {
case MUST:
return "MUST"
case SHOULD:
return "SHOULD"
default:
return ""
}
}
func termsString(ts []string) string {
if len(ts) == 0 {
return ""
}
sts := make([]string, len(ts))
for i, t := range ts {
sts[i] = fmt.Sprintf(`"%s"`, t)
}
return fmt.Sprintf(", Terms:[%s]", strings.Join(sts, ","))
}
func subqueriesString(qs []Query) string {
if len(qs) == 0 {
return ""
}
sqs := make([]string, len(qs))
for i, t := range qs {
sqs[i] = fmt.Sprintf(`%s`, t)
}
return fmt.Sprintf(", SubQueries:[%s]", strings.Join(sqs, ","))
}
func (q Query) String() string {
return fmt.Sprintf("{Occur:%s%s%s}", q.Occur.string(), termsString(q.Terms), subqueriesString(q.SubQueries))
}