Skip to content

Commit 4cc88fc

Browse files
committed
add auto-complete suggestions API
1 parent 41fd4a6 commit 4cc88fc

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

NRediSearch/Client.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,5 +323,98 @@ public async Task<long> OptimizeIndexAsync()
323323
{
324324
return (long) await _db.ExecuteAsync("FT.OPTIMIZE", _boxedIndexName).ConfigureAwait(false);
325325
}
326+
327+
/// <summary>
328+
/// Get the size of an autoc-complete suggestion dictionary
329+
/// </summary>
330+
public long CountSuggestions()
331+
=> (long)DbSync.Execute("FT.SUGLEN".Literal(), _boxedIndexName);
332+
333+
/// <summary>
334+
/// Get the size of an autoc-complete suggestion dictionary
335+
/// </summary>
336+
public async Task<long> CountSuggestionsAsync()
337+
=> (long)await _db.ExecuteAsync("FT.SUGLEN".Literal(), _boxedIndexName).ConfigureAwait(false);
338+
339+
/// <summary>
340+
/// Add a suggestion string to an auto-complete suggestion dictionary. This is disconnected from the index definitions, and leaves creating and updating suggestino dictionaries to the user.
341+
/// </summary>
342+
/// <param name="value">the suggestion string we index</param>
343+
/// <param name="score">a floating point number of the suggestion string's weight</param>
344+
/// <param name="increment">if set, we increment the existing entry of the suggestion by the given score, instead of replacing the score. This is useful for updating the dictionary based on user queries in real time</param>
345+
/// <returns>the current size of the suggestion dictionary.</returns>
346+
public long AddSuggestion(string value, double score, bool increment = false)
347+
{
348+
object args = increment
349+
? new object[] { _boxedIndexName, value, score, "INCR".Literal() }
350+
: new object[] { _boxedIndexName, value, score };
351+
return (long)DbSync.Execute("FT.SUGADD", args);
352+
}
353+
354+
/// <summary>
355+
/// Add a suggestion string to an auto-complete suggestion dictionary. This is disconnected from the index definitions, and leaves creating and updating suggestino dictionaries to the user.
356+
/// </summary>
357+
/// <param name="value">the suggestion string we index</param>
358+
/// <param name="score">a floating point number of the suggestion string's weight</param>
359+
/// <param name="increment">if set, we increment the existing entry of the suggestion by the given score, instead of replacing the score. This is useful for updating the dictionary based on user queries in real time</param>
360+
/// <returns>the current size of the suggestion dictionary.</returns>
361+
public async Task<long> AddSuggestionAsync(string value, double score, bool increment = false)
362+
{
363+
object args = increment
364+
? new object[] { _boxedIndexName, value, score, "INCR".Literal() }
365+
: new object[] { _boxedIndexName, value, score };
366+
return (long)await _db.ExecuteAsync("FT.SUGADD", args).ConfigureAwait(false);
367+
}
368+
369+
/// <summary>
370+
/// Delete a string from a suggestion index.
371+
/// </summary>
372+
/// <param name="value">the string to delete</param>
373+
public bool DeleteSuggestion(string value)
374+
=> (long)DbSync.Execute("FT.SUGDEL", _boxedIndexName, value) == 1;
375+
376+
/// <summary>
377+
/// Delete a string from a suggestion index.
378+
/// </summary>
379+
/// <param name="value">the string to delete</param>
380+
public async Task<bool> DeleteSuggestionAsync(string value)
381+
=> (long)await _db.ExecuteAsync("FT.SUGDEL", _boxedIndexName, value).ConfigureAwait(false) == 1;
382+
383+
/// <summary>
384+
/// Get completion suggestions for a prefix
385+
/// </summary>
386+
/// <param name="prefix">the prefix to complete on</param>
387+
/// <param name="fuzzy"> if set,we do a fuzzy prefix search, including prefixes at levenshtein distance of 1 from the prefix sent</param>
388+
/// <param name="max">If set, we limit the results to a maximum of num. (Note: The default is 5, and the number cannot be greater than 10).</param>
389+
/// <returns>a list of the top suggestions matching the prefix</returns>
390+
public string[] GetSuggestions(string prefix, bool fuzzy = false, int max = 5)
391+
{
392+
var args = new List<object> { _boxedIndexName, prefix};
393+
if (fuzzy) args.Add("FUZZY".Literal());
394+
if (max != 5)
395+
{
396+
args.Add("MAX".Literal());
397+
args.Add(max);
398+
}
399+
return (string[])DbSync.Execute("FT.SUGGET", args);
400+
}
401+
/// <summary>
402+
/// Get completion suggestions for a prefix
403+
/// </summary>
404+
/// <param name="prefix">the prefix to complete on</param>
405+
/// <param name="fuzzy"> if set,we do a fuzzy prefix search, including prefixes at levenshtein distance of 1 from the prefix sent</param>
406+
/// <param name="max">If set, we limit the results to a maximum of num. (Note: The default is 5, and the number cannot be greater than 10).</param>
407+
/// <returns>a list of the top suggestions matching the prefix</returns>
408+
public async Task<string[]> GetSuggestionsAsync(string prefix, bool fuzzy = false, int max = 5)
409+
{
410+
var args = new List<object> { _boxedIndexName, prefix };
411+
if (fuzzy) args.Add("FUZZY".Literal());
412+
if (max != 5)
413+
{
414+
args.Add("MAX".Literal());
415+
args.Add(max);
416+
}
417+
return (string[])await _db.ExecuteAsync("FT.SUGGET", args).ConfigureAwait(false);
418+
}
326419
}
327420
}

0 commit comments

Comments
 (0)