forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggester_context_category.go
154 lines (127 loc) · 3.87 KB
/
suggester_context_category.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
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
// -- SuggesterCategoryMapping --
// SuggesterCategoryMapping provides a mapping for a category context in a suggester.
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/suggester-context.html#_category_mapping.
type SuggesterCategoryMapping struct {
name string
fieldName string
defaultValues []string
}
// NewSuggesterCategoryMapping creates a new SuggesterCategoryMapping.
func NewSuggesterCategoryMapping(name string) *SuggesterCategoryMapping {
return &SuggesterCategoryMapping{
name: name,
defaultValues: make([]string, 0),
}
}
func (q *SuggesterCategoryMapping) DefaultValues(values ...string) *SuggesterCategoryMapping {
q.defaultValues = append(q.defaultValues, values...)
return q
}
func (q *SuggesterCategoryMapping) FieldName(fieldName string) *SuggesterCategoryMapping {
q.fieldName = fieldName
return q
}
// Source returns a map that will be used to serialize the context query as JSON.
func (q *SuggesterCategoryMapping) Source() (interface{}, error) {
source := make(map[string]interface{})
x := make(map[string]interface{})
source[q.name] = x
x["type"] = "category"
switch len(q.defaultValues) {
case 0:
x["default"] = q.defaultValues
case 1:
x["default"] = q.defaultValues[0]
default:
x["default"] = q.defaultValues
}
if q.fieldName != "" {
x["path"] = q.fieldName
}
return source, nil
}
// -- SuggesterCategoryQuery --
// SuggesterCategoryQuery provides querying a category context in a suggester.
// See https://www.elastic.co/guide/en/elasticsearch/reference/6.8/suggester-context.html#_category_query.
type SuggesterCategoryQuery struct {
name string
values map[string]*int
}
// NewSuggesterCategoryQuery creates a new SuggesterCategoryQuery.
func NewSuggesterCategoryQuery(name string, values ...string) *SuggesterCategoryQuery {
q := &SuggesterCategoryQuery{
name: name,
values: make(map[string]*int),
}
if len(values) > 0 {
q.Values(values...)
}
return q
}
func (q *SuggesterCategoryQuery) Value(val string) *SuggesterCategoryQuery {
q.values[val] = nil
return q
}
func (q *SuggesterCategoryQuery) ValueWithBoost(val string, boost int) *SuggesterCategoryQuery {
q.values[val] = &boost
return q
}
func (q *SuggesterCategoryQuery) Values(values ...string) *SuggesterCategoryQuery {
for _, val := range values {
q.values[val] = nil
}
return q
}
// Source returns a map that will be used to serialize the context query as JSON.
func (q *SuggesterCategoryQuery) Source() (interface{}, error) {
source := make(map[string]interface{})
switch len(q.values) {
case 0:
source[q.name] = make([]string, 0)
default:
contexts := make([]interface{}, 0)
for val, boost := range q.values {
context := make(map[string]interface{})
context["context"] = val
if boost != nil {
context["boost"] = *boost
}
contexts = append(contexts, context)
}
source[q.name] = contexts
}
return source, nil
}
type SuggesterCategoryIndex struct {
name string
values []string
}
// NewSuggesterCategoryIndex creates a new SuggesterCategoryIndex.
func NewSuggesterCategoryIndex(name string, values ...string) *SuggesterCategoryIndex {
q := &SuggesterCategoryIndex{
name: name,
values: values,
}
return q
}
func (q *SuggesterCategoryIndex) Values(values ...string) *SuggesterCategoryIndex {
q.values = append(q.values, values...)
return q
}
// Source returns a map that will be used to serialize the context query as JSON.
func (q *SuggesterCategoryIndex) Source() (interface{}, error) {
source := make(map[string]interface{})
switch len(q.values) {
case 0:
source[q.name] = make([]string, 0)
case 1:
source[q.name] = q.values[0]
default:
source[q.name] = q.values
}
return source, nil
}