Skip to content

Commit

Permalink
INCR Parameter to SUGADD (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: filipe oliveira <[email protected]>
  • Loading branch information
TropicalPenguin and filipecosta90 authored Jul 28, 2020
1 parent 3260786 commit 207c949
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions redisearch/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 23 additions & 1 deletion redisearch/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
1 change: 1 addition & 0 deletions redisearch/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 207c949

Please sign in to comment.