Skip to content

Commit

Permalink
Fix serialization of suggester with context queries
Browse files Browse the repository at this point in the history
Context queries of a suggester need to be serializes as a dictionary,
not an array.

See #329
  • Loading branch information
olivere committed Jul 27, 2016
1 parent 45e97db commit 5c0487e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions suggester_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions suggester_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5c0487e

Please sign in to comment.