diff --git a/redisearch/autocomplete.go b/redisearch/autocomplete.go index 1c020ff..88f1e96 100644 --- a/redisearch/autocomplete.go +++ b/redisearch/autocomplete.go @@ -44,6 +44,9 @@ func (a *Autocompleter) AddTerms(terms ...Suggestion) error { for _, term := range terms { args := redis.Args{a.name, term.Term, term.Score} + if term.Incr { + args = append(args, "INCR") + } if term.Payload != "" { args = append(args, "PAYLOAD", term.Payload) } diff --git a/redisearch/autocomplete_test.go b/redisearch/autocomplete_test.go index 5de6a0c..229ca25 100644 --- a/redisearch/autocomplete_test.go +++ b/redisearch/autocomplete_test.go @@ -61,7 +61,7 @@ func TestSuggest(t *testing.T) { terms := make([]Suggestion, 10) for i := 0; i < 10; i++ { terms[i] = Suggestion{Term: fmt.Sprintf("foo %d", i), - Score: 1.0, Payload: fmt.Sprintf("bar %d", i)} + Score: 1.0, Payload: fmt.Sprintf("bar %d", i), Incr: true} } err := a.AddTerms(terms...) assert.Nil(t, err) @@ -87,4 +87,26 @@ func TestSuggest(t *testing.T) { assert.Contains(t, suggestion.Payload, "bar") assert.NotZero(t, suggestion.Score) } + + // Ensure score is incremented according to INCR + oldScore := suggestions[0].Score + + err = a.AddTerms(terms...) + suggestions, err = a.SuggestOpts("f", SuggestOptions{Num: 10, WithScores: true, WithPayloads: true}) + assert.Nil(t, err) + for _, suggestion := range suggestions { + assert.Greater(t, suggestion.Score, oldScore) + } + + // Now, replace scores by disabling INCR + for i := 0; i < len(terms); i++ { + terms[i].Incr = false + } + + err = a.AddTerms(terms...) + suggestions, err = a.SuggestOpts("f", SuggestOptions{Num: 10, WithScores: true, WithPayloads: true}) + assert.Nil(t, err) + for _, suggestion := range suggestions { + assert.Equal(t, suggestion.Score, oldScore) + } } diff --git a/redisearch/suggest.go b/redisearch/suggest.go index ae48227..48238b2 100644 --- a/redisearch/suggest.go +++ b/redisearch/suggest.go @@ -7,6 +7,7 @@ type Suggestion struct { Term string Score float64 Payload string + Incr bool } // SuggestOptions are options which are passed when recieving suggestions from the Autocompleter