Debian Codegraph @

suggester_context_geo.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 // -- SuggesterGeoMapping --
8
9 // SuggesterGeoMapping provides a mapping for a geolocation context in a suggester.
10 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_geo_location_mapping.
11 type SuggesterGeoMapping struct {
12 name string
13 defaultLocations []*GeoPoint
14 precision []string
15 neighbors *bool
16 fieldName string
17 }
18
19 // NewSuggesterGeoMapping creates a new SuggesterGeoMapping.
20 func NewSuggesterGeoMapping(name string) *SuggesterGeoMapping {
21 return &SuggesterGeoMapping{
22 name: name,
23 }
24 }
25
26 func (q *SuggesterGeoMapping) DefaultLocations(locations ...*GeoPoint) *SuggesterGeoMapping {
27 q.defaultLocations = append(q.defaultLocations, locations...)
28 return q
29 }
30
31 func (q *SuggesterGeoMapping) Precision(precision ...string) *SuggesterGeoMapping {
32 q.precision = append(q.precision, precision...)
33 return q
34 }
35
36 func (q *SuggesterGeoMapping) Neighbors(neighbors bool) *SuggesterGeoMapping {
37 q.neighbors = &neighbors
38 return q
39 }
40
41 func (q *SuggesterGeoMapping) FieldName(fieldName string) *SuggesterGeoMapping {
42 q.fieldName = fieldName
43 return q
44 }
45
46 // Source returns a map that will be used to serialize the context query as JSON.
47 func (q *SuggesterGeoMapping) Source() (interface{}, error) {
48 source := make(map[string]interface{})
49
50 x := make(map[string]interface{})
51 source[q.name] = x
52
53 x["type"] = "geo"
54
55 if len(q.precision) > 0 {
56 x["precision"] = q.precision
57 }
58 if q.neighbors != nil {
59 x["neighbors"] = *q.neighbors
60 }
61
62 switch len(q.defaultLocations) {
63 case 0:
64 case 1:
65 x["default"] = q.defaultLocations[0].Source()
66 default:
67 var arr []interface{}
68 for _, p := range q.defaultLocations {
69 arr = append(arr, p.Source())
70 }
71 x["default"] = arr
72 }
73
74 if q.fieldName != "" {
75 x["path"] = q.fieldName
76 }
77 return source, nil
78 }
79
80 // -- SuggesterGeoQuery --
81
82 // SuggesterGeoQuery provides querying a geolocation context in a suggester.
83 // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_geo_location_query
84 type SuggesterGeoQuery struct {
85 name string
86 location *GeoPoint
87 precision []string
88 }
89
90 // NewSuggesterGeoQuery creates a new SuggesterGeoQuery.
91 func NewSuggesterGeoQuery(name string, location *GeoPoint) *SuggesterGeoQuery {
92 return &SuggesterGeoQuery{
93 name: name,
94 location: location,
95 precision: make([]string, 0),
96 }
97 }
98
99 func (q *SuggesterGeoQuery) Precision(precision ...string) *SuggesterGeoQuery {
100 q.precision = append(q.precision, precision...)
101 return q
102 }
103
104 // Source returns a map that will be used to serialize the context query as JSON.
105 func (q *SuggesterGeoQuery) Source() (interface{}, error) {
106 source := make(map[string]interface{})
107
108 if len(q.precision) == 0 {
109 if q.location != nil {
110 source[q.name] = q.location.Source()
111 }
112 } else {
113 x := make(map[string]interface{})
114 source[q.name] = x
115
116 if q.location != nil {
117 x["value"] = q.location.Source()
118 }
119
120 switch len(q.precision) {
121 case 0:
122 case 1:
123 x["precision"] = q.precision[0]
124 default:
125 x["precision"] = q.precision
126 }
127 }
128
129 return source, nil
130 }