From 5c0487e35932b92698562f74559cdcd9cd0836fb Mon Sep 17 00:00:00 2001 From: Oliver Eilhard Date: Wed, 27 Jul 2016 19:13:20 +0200 Subject: [PATCH] Fix serialization of suggester with context queries Context queries of a suggester need to be serializes as a dictionary, not an array. See #329 --- CONTRIBUTORS | 1 + client.go | 2 +- suggester_completion.go | 13 +++++++++++-- suggester_completion_test.go | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7f17307d8..15b8aa18e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -59,3 +59,4 @@ TimeEmit [@TimeEmit](https://github.com/timeemit) TusharM [@tusharm](https://github.com/tusharm) wolfkdy [@wolfkdy](https://github.com/wolfkdy) zakthomas [@zakthomas](https://github.com/zakthomas) +singham [@zhaochenxiao90](https://github.com/zhaochenxiao90) diff --git a/client.go b/client.go index 70d62f0ee..7b1ca3253 100644 --- a/client.go +++ b/client.go @@ -21,7 +21,7 @@ import ( const ( // Version is the current version of Elastic. - Version = "2.0.51" + Version = "2.0.52" // DefaultUrl is the default endpoint of Elasticsearch on the local machine. // It is used e.g. when initializing a new Client without a specific URL. diff --git a/suggester_completion.go b/suggester_completion.go index e38c38fc4..c8e89518b 100644 --- a/suggester_completion.go +++ b/suggester_completion.go @@ -102,9 +102,18 @@ func (q CompletionSuggester) Source(includeName bool) interface{} { case 1: suggester["context"] = q.contextQueries[0].Source() default: - ctxq := make([]interface{}, 0) + ctxq := make(map[string]interface{}) for _, query := range q.contextQueries { - ctxq = append(ctxq, query.Source()) + src := query.Source() + // Merge the dictionary into ctxq + m, ok := src.(map[string]interface{}) + if !ok { + // We have no way of reporting errors in v2, so we just swallow it. + continue + } + for k, v := range m { + ctxq[k] = v + } } suggester["context"] = ctxq } diff --git a/suggester_completion_test.go b/suggester_completion_test.go index 18d874590..82a4d14eb 100644 --- a/suggester_completion_test.go +++ b/suggester_completion_test.go @@ -23,3 +23,22 @@ func TestCompletionSuggesterSource(t *testing.T) { t.Errorf("expected\n%s\n,got:\n%s", expected, got) } } + +func TestCompletionSuggesterSourceWithMultipleContexts(t *testing.T) { + s := NewCompletionSuggester("song-suggest"). + Text("n"). + Field("suggest"). + ContextQueries( + NewSuggesterCategoryQuery("artist", "Sting"), + NewSuggesterCategoryQuery("label", "BMG"), + ) + data, err := json.Marshal(s.Source(true)) + if err != nil { + t.Fatalf("marshaling to JSON failed: %v", err) + } + got := string(data) + expected := `{"song-suggest":{"text":"n","completion":{"context":{"artist":"Sting","label":"BMG"},"field":"suggest"}}}` + if got != expected { + t.Errorf("expected\n%s\n,got:\n%s", expected, got) + } +}