Debian Codegraph @

suggester_context_category.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 // -- SuggesterCategoryMapping --
8
9 // SuggesterCategoryMapping provides a mapping for a category context in a suggester.
10 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_category_mapping.
11 type SuggesterCategoryMapping struct {
12 name string
13 fieldName string
14 defaultValues []string
15 }
16
17 // NewSuggesterCategoryMapping creates a new SuggesterCategoryMapping.
18 func NewSuggesterCategoryMapping(name string) *SuggesterCategoryMapping {
19 return &SuggesterCategoryMapping{
20 name: name,
21 defaultValues: make([]string, 0),
22 }
23 }
24
25 func (q *SuggesterCategoryMapping) DefaultValues(values ...string) *SuggesterCategoryMapping {
26 q.defaultValues = append(q.defaultValues, values...)
27 return q
28 }
29
30 func (q *SuggesterCategoryMapping) FieldName(fieldName string) *SuggesterCategoryMapping {
31 q.fieldName = fieldName
32 return q
33 }
34
35 // Source returns a map that will be used to serialize the context query as JSON.
36 func (q *SuggesterCategoryMapping) Source() (interface{}, error) {
37 source := make(map[string]interface{})
38
39 x := make(map[string]interface{})
40 source[q.name] = x
41
42 x["type"] = "category"
43
44 switch len(q.defaultValues) {
45 case 0:
46 x["default"] = q.defaultValues
47 case 1:
48 x["default"] = q.defaultValues[0]
49 default:
50 x["default"] = q.defaultValues
51 }
52
53 if q.fieldName != "" {
54 x["path"] = q.fieldName
55 }
56 return source, nil
57 }
58
59 // -- SuggesterCategoryQuery --
60
61 // SuggesterCategoryQuery provides querying a category context in a suggester.
62 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_category_query.
63 type SuggesterCategoryQuery struct {
64 name string
65 values map[string]*int
66 }
67
68 // NewSuggesterCategoryQuery creates a new SuggesterCategoryQuery.
69 func NewSuggesterCategoryQuery(name string, values ...string) *SuggesterCategoryQuery {
70 q := &SuggesterCategoryQuery{
71 name: name,
72 values: make(map[string]*int),
73 }
74
75 if len(values) > 0 {
76 q.Values(values...)
77 }
78 return q
79 }
80
81 func (q *SuggesterCategoryQuery) Value(val string) *SuggesterCategoryQuery {
82 q.values[val] = nil
83 return q
84 }
85
86 func (q *SuggesterCategoryQuery) ValueWithBoost(val string, boost int) *SuggesterCategoryQuery {
87 q.values[val] = &boost
88 return q
89 }
90
91 func (q *SuggesterCategoryQuery) Values(values ...string) *SuggesterCategoryQuery {
92 for _, val := range values {
93 q.values[val] = nil
94 }
95 return q
96 }
97
98 // Source returns a map that will be used to serialize the context query as JSON.
99 func (q *SuggesterCategoryQuery) Source() (interface{}, error) {
100 source := make(map[string]interface{})
101
102 switch len(q.values) {
103 case 0:
104 source[q.name] = make([]string, 0)
105 default:
106 contexts := make([]interface{}, 0)
107 for val, boost := range q.values {
108 context := make(map[string]interface{})
109 context["context"] = val
110 if boost != nil {
111 context["boost"] = *boost
112 }
113 contexts = append(contexts, context)
114 }
115 source[q.name] = contexts
116 }
117
118 return source, nil
119 }