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 2de6a99 commit 787ebbb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 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 = "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.
Expand Down
15 changes: 12 additions & 3 deletions suggester_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions suggester_completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 787ebbb

Please sign in to comment.