@@ -7,8 +7,13 @@ import (
77
88// Autocompleter implements a redisearch auto-completer API 
99type  Autocompleter  struct  {
10- 	pool  * redis.Pool 
1110	name  string 
11+ 	pool  * redis.Pool 
12+ }
13+ 
14+ // NewAutocompleter creates a new Autocompleter with the given pool and key name 
15+ func  NewAutocompleterFromPool (pool  * redis.Pool , name  string ) * Autocompleter  {
16+ 	return  & Autocompleter {name : name , pool : pool }
1217}
1318
1419// NewAutocompleter creates a new Autocompleter with the given host and key name 
@@ -23,7 +28,6 @@ func NewAutocompleter(addr, name string) *Autocompleter {
2328
2429// Delete deletes the Autocompleter key for this AC 
2530func  (a  * Autocompleter ) Delete () error  {
26- 
2731	conn  :=  a .pool .Get ()
2832	defer  conn .Close ()
2933
@@ -33,7 +37,6 @@ func (a *Autocompleter) Delete() error {
3337
3438// AddTerms pushes new term suggestions to the index 
3539func  (a  * Autocompleter ) AddTerms (terms  ... Suggestion ) error  {
36- 
3740	conn  :=  a .pool .Get ()
3841	defer  conn .Close ()
3942
@@ -62,49 +65,85 @@ func (a *Autocompleter) AddTerms(terms ...Suggestion) error {
6265	return  nil 
6366}
6467
68+ // AddTerms pushes new term suggestions to the index 
69+ func  (a  * Autocompleter ) DeleteTerms (terms  ... Suggestion ) error  {
70+ 	conn  :=  a .pool .Get ()
71+ 	defer  conn .Close ()
72+ 
73+ 	i  :=  0 
74+ 	for  _ , term  :=  range  terms  {
75+ 
76+ 		args  :=  redis.Args {a .name , term .Term }
77+ 		if  err  :=  conn .Send ("FT.SUGDEL" , args ... ); err  !=  nil  {
78+ 			return  err 
79+ 		}
80+ 		i ++ 
81+ 	}
82+ 	if  err  :=  conn .Flush (); err  !=  nil  {
83+ 		return  err 
84+ 	}
85+ 	for  i  >  0  {
86+ 		if  _ , err  :=  conn .Receive (); err  !=  nil  {
87+ 			return  err 
88+ 		}
89+ 		i -- 
90+ 	}
91+ 	return  nil 
92+ }
93+ 
94+ // AddTerms pushes new term suggestions to the index 
95+ func  (a  * Autocompleter ) Length () (len  int64 , err  error ) {
96+ 	conn  :=  a .pool .Get ()
97+ 	defer  conn .Close ()
98+ 	len , err  =  redis .Int64 (conn .Do ("FT.SUGLEN" , a .name ))
99+ 	return 
100+ }
101+ 
65102// Suggest gets completion suggestions from the Autocompleter dictionary to the given prefix. 
66103// If fuzzy is set, we also complete for prefixes that are in 1 Levenshten distance from the 
67104// given prefix 
68105// 
69106// Deprecated: Please use SuggestOpts() instead 
70- func  (a  * Autocompleter ) Suggest (prefix  string , num  int , fuzzy  bool ) ([]Suggestion , error ) {
107+ func  (a  * Autocompleter ) Suggest (prefix  string , num  int , fuzzy  bool ) (ret   []Suggestion ,  err  error ) {
71108	conn  :=  a .pool .Get ()
72109	defer  conn .Close ()
73110
74- 	args  :=  redis.Args {a .name , prefix , "MAX" , num , "WITHSCORES" }
75- 	if  fuzzy  {
76- 		args  =  append (args , "FUZZY" )
77- 	}
111+ 	seropts  :=  DefaultSuggestOptions 
112+ 	seropts .Num  =  num 
113+ 	seropts .Fuzzy  =  fuzzy 
114+ 	args , inc  :=  a .Serialize (prefix , seropts )
115+ 
78116	vals , err  :=  redis .Strings (conn .Do ("FT.SUGGET" , args ... ))
79117	if  err  !=  nil  {
80118		return  nil , err 
81119	}
82120
83- 	ret  :=  make ([]Suggestion , 0 , len (vals )/ 2 )
84- 	for  i  :=  0 ; i  <  len (vals ); i  +=  2  {
85- 
86- 		score , err  :=  strconv .ParseFloat (vals [i + 1 ], 64 )
87- 		if  err  !=  nil  {
88- 			continue 
89- 		}
90- 		ret  =  append (ret , Suggestion {Term : vals [i ], Score : score })
91- 
92- 	}
93- 
94- 	return  ret , nil 
121+ 	ret  =  ProcessSugGetVals (vals , inc , true , false )
95122
123+ 	return 
96124}
97125
98126// SuggestOpts gets completion suggestions from the Autocompleter dictionary to the given prefix. 
99127// SuggestOptions are passed allowing you specify if the returned values contain a payload, and scores. 
100- // If SuggestOptions.Fuzzy is set, we also complete for prefixes that are in 1 Levenshten  distance from the 
128+ // If SuggestOptions.Fuzzy is set, we also complete for prefixes that are in 1 Levenshtein  distance from the 
101129// given prefix 
102- func  (a  * Autocompleter ) SuggestOpts (prefix  string , opts  SuggestOptions ) ([]Suggestion , error ) {
130+ func  (a  * Autocompleter ) SuggestOpts (prefix  string , opts  SuggestOptions ) (ret   []Suggestion ,  err  error ) {
103131	conn  :=  a .pool .Get ()
104132	defer  conn .Close ()
105133
106- 	inc  :=  1 
134+ 	args , inc  :=  a .Serialize (prefix , opts )
135+ 	vals , err  :=  redis .Strings (conn .Do ("FT.SUGGET" , args ... ))
136+ 	if  err  !=  nil  {
137+ 		return  nil , err 
138+ 	}
139+ 
140+ 	ret  =  ProcessSugGetVals (vals , inc , opts .WithScores , opts .WithPayloads )
107141
142+ 	return 
143+ }
144+ 
145+ func  (a  * Autocompleter ) Serialize (prefix  string , opts  SuggestOptions ) (redis.Args , int ) {
146+ 	inc  :=  1 
108147	args  :=  redis.Args {a .name , prefix , "MAX" , opts .Num }
109148	if  opts .Fuzzy  {
110149		args  =  append (args , "FUZZY" )
@@ -117,29 +156,26 @@ func (a *Autocompleter) SuggestOpts(prefix string, opts SuggestOptions) ([]Sugge
117156		args  =  append (args , "WITHPAYLOADS" )
118157		inc ++ 
119158	}
120- 	vals , err  :=  redis .Strings (conn .Do ("FT.SUGGET" , args ... ))
121- 	if  err  !=  nil  {
122- 		return  nil , err 
123- 	}
159+ 	return  args , inc 
160+ }
124161
125- 	ret  :=  make ([]Suggestion , 0 , len (vals )/ inc )
162+ func  ProcessSugGetVals (vals  []string , inc  int , WithScores , WithPayloads  bool ) (ret  []Suggestion ) {
163+ 	ret  =  make ([]Suggestion , 0 , len (vals )/ inc )
126164	for  i  :=  0 ; i  <  len (vals ); i  +=  inc  {
127165
128166		suggestion  :=  Suggestion {Term : vals [i ]}
129- 		if  opts . WithScores  {
167+ 		if  WithScores  {
130168			score , err  :=  strconv .ParseFloat (vals [i + 1 ], 64 )
131169			if  err  !=  nil  {
132170				continue 
133171			}
134172			suggestion .Score  =  score 
135173		}
136- 		if  opts . WithPayloads  {
174+ 		if  WithPayloads  {
137175			suggestion .Payload  =  vals [i + (inc - 1 )]
138176		}
139177		ret  =  append (ret , suggestion )
140178
141179	}
142- 
143- 	return  ret , nil 
144- 
180+ 	return 
145181}
0 commit comments