Browse Source

Fix suggester context query

Fixes to suggester context query.

Close #835
Muhammet Çakır 10 months ago
parent
commit
e82c7f320e

+ 35
- 0
suggester_context_category.go View File

@@ -117,3 +117,38 @@ func (q *SuggesterCategoryQuery) Source() (interface{}, error) {
117 117
 
118 118
 	return source, nil
119 119
 }
120
+
121
+type SuggesterCategoryIndex struct {
122
+	name   string
123
+	values []string
124
+}
125
+
126
+// NewSuggesterCategoryIndex creates a new SuggesterCategoryIndex.
127
+func NewSuggesterCategoryIndex(name string, values ...string) *SuggesterCategoryIndex {
128
+	q := &SuggesterCategoryIndex{
129
+		name:   name,
130
+		values: values,
131
+	}
132
+	return q
133
+}
134
+
135
+func (q *SuggesterCategoryIndex) Values(values ...string) *SuggesterCategoryIndex {
136
+	q.values = append(q.values, values...)
137
+	return q
138
+}
139
+
140
+// Source returns a map that will be used to serialize the context query as JSON.
141
+func (q *SuggesterCategoryIndex) Source() (interface{}, error) {
142
+	source := make(map[string]interface{})
143
+
144
+	switch len(q.values) {
145
+	case 0:
146
+		source[q.name] = make([]string, 0)
147
+	case 1:
148
+		source[q.name] = q.values[0]
149
+	default:
150
+		source[q.name] = q.values
151
+	}
152
+
153
+	return source, nil
154
+}

+ 34
- 0
suggester_context_category_test.go View File

@@ -161,3 +161,37 @@ func TestSuggesterCategoryQueryWithoutBoost(t *testing.T) {
161 161
 		t.Errorf("expected any of %v\n,got:\n%s", expectedOutcomes, got)
162 162
 	}
163 163
 }
164
+
165
+func TestSuggesterCategoryIndex(t *testing.T) {
166
+	in := NewSuggesterCategoryIndex("color", "red")
167
+	src, err := in.Source()
168
+	if err != nil {
169
+		t.Fatal(err)
170
+	}
171
+	data, err := json.Marshal(src)
172
+	if err != nil {
173
+		t.Fatalf("marshaling to JSON failed: %v", err)
174
+	}
175
+	got := string(data)
176
+	expected := `{"color":"red"}`
177
+	if got != expected {
178
+		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
179
+	}
180
+}
181
+
182
+func TestSuggesterCategoryIndexWithTwoValues(t *testing.T) {
183
+	in := NewSuggesterCategoryIndex("color", "red", "yellow")
184
+	src, err := in.Source()
185
+	if err != nil {
186
+		t.Fatal(err)
187
+	}
188
+	data, err := json.Marshal(src)
189
+	if err != nil {
190
+		t.Fatalf("marshaling to JSON failed: %v", err)
191
+	}
192
+	got := string(data)
193
+	expected := `{"color":["red","yellow"]}`
194
+	if got != expected {
195
+		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
196
+	}
197
+}

+ 72
- 24
suggester_context_geo.go View File

@@ -82,22 +82,34 @@ func (q *SuggesterGeoMapping) Source() (interface{}, error) {
82 82
 // SuggesterGeoQuery provides querying a geolocation context in a suggester.
83 83
 // See https://www.elastic.co/guide/en/elasticsearch/reference/6.2/suggester-context.html#_geo_location_query
84 84
 type SuggesterGeoQuery struct {
85
-	name      string
86
-	location  *GeoPoint
87
-	precision []string
85
+	name       string
86
+	location   *GeoPoint
87
+	precision  string
88
+	neighbours []string
89
+	boost      *int
88 90
 }
89 91
 
90 92
 // NewSuggesterGeoQuery creates a new SuggesterGeoQuery.
91 93
 func NewSuggesterGeoQuery(name string, location *GeoPoint) *SuggesterGeoQuery {
92 94
 	return &SuggesterGeoQuery{
93
-		name:      name,
94
-		location:  location,
95
-		precision: make([]string, 0),
95
+		name:       name,
96
+		location:   location,
97
+		neighbours: make([]string, 0),
96 98
 	}
97 99
 }
98 100
 
99
-func (q *SuggesterGeoQuery) Precision(precision ...string) *SuggesterGeoQuery {
100
-	q.precision = append(q.precision, precision...)
101
+func (q *SuggesterGeoQuery) Precision(precision string) *SuggesterGeoQuery {
102
+	q.precision = precision
103
+	return q
104
+}
105
+
106
+func (q *SuggesterGeoQuery) Neighbours(neighbours ...string) *SuggesterGeoQuery {
107
+	q.neighbours = append(q.neighbours, neighbours...)
108
+	return q
109
+}
110
+
111
+func (q *SuggesterGeoQuery) Boost(boost int) *SuggesterGeoQuery {
112
+	q.boost = &boost
101 113
 	return q
102 114
 }
103 115
 
@@ -105,25 +117,61 @@ func (q *SuggesterGeoQuery) Precision(precision ...string) *SuggesterGeoQuery {
105 117
 func (q *SuggesterGeoQuery) Source() (interface{}, error) {
106 118
 	source := make(map[string]interface{})
107 119
 
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
120
+	x := make(map[string]interface{})
121
+	source[q.name] = x
115 122
 
116
-		if q.location != nil {
117
-			x["value"] = q.location.Source()
118
-		}
123
+	if q.location != nil {
124
+		x["context"] = q.location.Source()
125
+	}
126
+	if q.precision != "" {
127
+		x["precision"] = q.precision
128
+	}
129
+	if q.boost != nil {
130
+		x["boost"] = q.boost
131
+	}
132
+	switch len(q.neighbours) {
133
+	case 0:
134
+	case 1:
135
+		x["neighbours"] = q.neighbours[0]
136
+	default:
137
+		x["neighbours"] = q.neighbours
138
+	}
139
+
140
+	return source, nil
141
+}
142
+
143
+type SuggesterGeoIndex struct {
144
+	name      string
145
+	locations []*GeoPoint
146
+}
119 147
 
120
-		switch len(q.precision) {
121
-		case 0:
122
-		case 1:
123
-			x["precision"] = q.precision[0]
124
-		default:
125
-			x["precision"] = q.precision
148
+// NewSuggesterGeoQuery creates a new SuggesterGeoQuery.
149
+func NewSuggesterGeoIndex(name string) *SuggesterGeoIndex {
150
+	return &SuggesterGeoIndex{
151
+		name: name,
152
+	}
153
+}
154
+
155
+func (q *SuggesterGeoIndex) Locations(locations ...*GeoPoint) *SuggesterGeoIndex {
156
+	q.locations = append(q.locations, locations...)
157
+	return q
158
+}
159
+
160
+// Source returns a map that will be used to serialize the context query as JSON.
161
+func (q *SuggesterGeoIndex) Source() (interface{}, error) {
162
+	source := make(map[string]interface{})
163
+
164
+	switch len(q.locations) {
165
+	case 0:
166
+		source[q.name] = make([]string, 0)
167
+	case 1:
168
+		source[q.name] = q.locations[0].Source()
169
+	default:
170
+		var arr []interface{}
171
+		for _, p := range q.locations {
172
+			arr = append(arr, p.Source())
126 173
 		}
174
+		source[q.name] = arr
127 175
 	}
128 176
 
129 177
 	return source, nil

+ 48
- 2
suggester_context_geo_test.go View File

@@ -31,17 +31,63 @@ func TestSuggesterGeoMapping(t *testing.T) {
31 31
 }
32 32
 
33 33
 func TestSuggesterGeoQuery(t *testing.T) {
34
-	q := NewSuggesterGeoQuery("location", GeoPointFromLatLon(11.5, 62.71)).Precision("1km")
34
+	q := NewSuggesterGeoQuery("location", GeoPointFromLatLon(11.5, 62.71)).Precision("1km").
35
+		Neighbours("2km", "3km").Boost(2)
35 36
 	src, err := q.Source()
36 37
 	if err != nil {
37 38
 		t.Fatal(err)
38 39
 	}
40
+
41
+	data, err := json.Marshal(src)
42
+	if err != nil {
43
+		t.Fatalf("marshaling to JSON failed: %v", err)
44
+	}
45
+	got := string(data)
46
+	expectedOutcomes := []string{
47
+		`{"location":{"context":{"lat":11.5,"lon":62.71},"precision":"1km","boost":2,"neighbours":["2km","3km"]}}`,
48
+		`{"location":{"boost":2,"context":{"lat":11.5,"lon":62.71},"neighbours":["2km","3km"],"precision":"1km"}}`,
49
+	}
50
+	var match bool
51
+	for _, expected := range expectedOutcomes {
52
+		if got == expected {
53
+			match = true
54
+			break
55
+		}
56
+	}
57
+	if !match {
58
+		t.Errorf("expected any of %v\n,got:\n%s", expectedOutcomes, got)
59
+	}
60
+}
61
+
62
+func TestSuggesterGeoIndex(t *testing.T) {
63
+	in := NewSuggesterGeoIndex("location").Locations(GeoPointFromLatLon(11.5, 62.71))
64
+	src, err := in.Source()
65
+	if err != nil {
66
+		t.Fatal(err)
67
+	}
68
+	data, err := json.Marshal(src)
69
+	if err != nil {
70
+		t.Fatalf("marshaling to JSON failed: %v", err)
71
+	}
72
+	got := string(data)
73
+	expected := `{"location":{"lat":11.5,"lon":62.71}}`
74
+	if got != expected {
75
+		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
76
+	}
77
+}
78
+
79
+func TestSuggesterGeoIndexWithTwoValues(t *testing.T) {
80
+	in := NewSuggesterGeoIndex("location").Locations(GeoPointFromLatLon(11.5, 62.71), GeoPointFromLatLon(31.5, 22.71))
81
+	src, err := in.Source()
82
+	if err != nil {
83
+		t.Fatal(err)
84
+	}
39 85
 	data, err := json.Marshal(src)
40 86
 	if err != nil {
41 87
 		t.Fatalf("marshaling to JSON failed: %v", err)
42 88
 	}
43 89
 	got := string(data)
44
-	expected := `{"location":{"precision":"1km","value":{"lat":11.5,"lon":62.71}}}`
90
+	expected := `{"location":[{"lat":11.5,"lon":62.71},{"lat":31.5,"lon":22.71}]}`
45 91
 	if got != expected {
46 92
 		t.Errorf("expected\n%s\n,got:\n%s", expected, got)
47 93
 	}

Loading…
Cancel
Save