[フレーム]
Docs Pricing
Login Book a meeting Try Redis

Autocomplete with Redis

Learn how to use the autocomplete feature of Redis for efficient prefix-based suggestion retrieval.

Overview

Redis Query Engine provides an autocomplete feature using suggestions that are stored in a trie-based data structure. This feature allows you to store and retrieve ranked suggestions based on user input prefixes, making it useful for applications like search boxes, command completion, and chatbot responses.

This guide covers how to use the FT.SUGADD, FT.SUGGET, FT.SUGDEL, and FT.SUGLEN commands to implement autocomplete, and some examples of how you can use these commands with FT.SEARCH.

Add autocomplete suggestions

To add phrases or words to a suggestions dictionary, use the FT.SUGADD command. You will assign a score to each entry, which determines its ranking in the results.

FT.SUGADD autocomplete "hello world" 100
FT.SUGADD autocomplete "hello there" 90
FT.SUGADD autocomplete "help me" 80
FT.SUGADD autocomplete "hero" 70

Integer scores were used in the above examples, but the scores argument is described as being floating point. Integer scores are converted to floating point internally. Also, "autocomplete" in the above examples is just the name of the key; you can use any key name you wish.

Optional arguments

The FT.SUGADD command can take two optional arguments:

Retrieve suggestions

To get autocomplete suggestions for a given prefix, use the FT.SUGGET command.

redis> FT.SUGGET autocomplete "he"
1) "hero"
2) "help me"
3) "hello world"
4) "hello there"

If you wish to see the scores, use the WITHSCORES option:

redis> FT.SUGGET autocomplete "he" WITHSCORES
1) "hero"
2) "40.414520263671875"
3) "help me"
4) "32.65986251831055"
5) "hello world"
6) "31.62277603149414"
7) "hello there"
8) "28.460498809814453"

Enable fuzzy matching

If you want to allow for small spelling mistakes or typos, use the FUZZY option. This option performs a fuzzy prefix search, including prefixes at a Levenshtein distance of 1 from the provided prefix.

redis> FT.SUGGET autocomplete hell FUZZY
1) "hello world"
2) "hello there"
3) "help me"

Optional arguments

There are three additional arguments you can use with FT.SUGGET:

Delete suggestions

To remove a specific suggestion from the dictionary, use the FT.SUGDEL command.

redis> FT.SUGDEL autocomplete "help me"
(integer) 1

After deletion, running FT.SUGGET autocomplete hell FUZZY will no longer return "help me".

Check the number of suggestions

To get a count of the number of entries in a given suggestions dictionary, use the FT.SUGLEN command.

redis> FT.SUGLEN autocomplete
(integer) 3

A common approach is to:

  1. Use FT.SUGGET to suggest query completions as users type in a text field.
  2. Once the user selects a suggestion, run FT.SEARCH using the selected term to get full search results.

Example workflow

  1. Get suggestions for a given user input.

    FT.SUGGET autocomplete "hel"
    
  2. Capture the user's selection.

  3. Use the selected suggestion in a full-text search.

    FT.SEARCH index "hello world"
    

Autocomplete use cases

The autocomplete feature in Redis Query Engine is useful for:

RATE THIS PAGE
Back to top ↑

On this page

AltStyle によって変換されたページ (->オリジナル) /