Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INCR Parameter to SUGADD #74

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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