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 63cdca667..faf04e0ac 100644 --- a/client.go +++ b/client.go @@ -21,7 +21,7 @@ import ( const ( // Version is the current version of Elastic. - Version = "3.0.44" + Version = "3.0.45" // 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 e0f5a3861..b7959f1f8 100644 --- a/suggester_completion.go +++ b/suggester_completion.go @@ -4,6 +4,8 @@ package elastic +import "errors" + // CompletionSuggester is a fast suggester for e.g. type-ahead completion. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html // for more details. @@ -74,7 +76,7 @@ type completionSuggesterRequest struct { Completion interface{} `json:"completion"` } -// Creates the source for the completion suggester. +// Source creates the JSON structure for the completion suggester. func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) { cs := &completionSuggesterRequest{} @@ -106,13 +108,20 @@ func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) { } suggester["context"] = src default: - ctxq := make([]interface{}, 0) + ctxq := make(map[string]interface{}) for _, query := range q.contextQueries { src, err := query.Source() if err != nil { return nil, err } - ctxq = append(ctxq, src) + // Merge the dictionary into ctxq + m, ok := src.(map[string]interface{}) + if !ok { + return nil, errors.New("elastic: context query is not a map") + } + for k, v := range m { + ctxq[k] = v + } } suggester["context"] = ctxq } diff --git a/suggester_completion_test.go b/suggester_completion_test.go index 986d3da01..e01b73482 100644 --- a/suggester_completion_test.go +++ b/suggester_completion_test.go @@ -27,3 +27,26 @@ 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"), + ) + src, err := s.Source(true) + if err != nil { + t.Fatal(err) + } + data, err := json.Marshal(src) + 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) + } +}