|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Learn how to use the autocomplete feature of Redis for efficient prefix-based suggestion retrieval. |
| 13 | +linkTitle: Autocomplete |
| 14 | +title: Autocomplete with Redis |
| 15 | +weight: 1 |
| 16 | +--- |
| 17 | + |
| 18 | +## Overview |
| 19 | + |
| 20 | +Redis Query Engine provides an autocomplete feature using suggestions that are stored in a [trie-based](https://en.wikipedia.org/wiki/Trie) data structure. |
| 21 | +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. |
| 22 | + |
| 23 | +This guide covers how to use the [`FT.SUGADD`]({{< baseurl >}}/commands/ft.sugadd), [`FT.SUGGET`]({{< baseurl >}}/commands/ft.sugget), [`FT.SUGDEL`]({{< baseurl >}}/commands/ft.sugdel), and [`FT.SUGLEN`]({{< baseurl >}}/commands/ft.suglen) commands to implement autocomplete, and some examples of how you can use these commands with [`FT.SEARCH`]({{< baseurl >}}/commands/ft.search). |
| 24 | + |
| 25 | +## Add autocomplete suggestions |
| 26 | + |
| 27 | +To add phrases or words to a suggestions dictionary, use the [`FT.SUGADD`]({{< baseurl >}}/commands/ft.sugadd) command. |
| 28 | +You will assign a score to each entry, which determines its ranking in the results. |
| 29 | + |
| 30 | +``` |
| 31 | +FT.SUGADD autocomplete "hello world" 100 |
| 32 | +FT.SUGADD autocomplete "hello there" 90 |
| 33 | +FT.SUGADD autocomplete "help me" 80 |
| 34 | +FT.SUGADD autocomplete "hero" 70 |
| 35 | +``` |
| 36 | + |
| 37 | +Integer scores were used in the above examples, but the scores argument is described as being floating point. |
| 38 | +Integer scores are converted to floating point internally. |
| 39 | +Also, "`autocomplete`" in the above examples is just the name of the key; you can use any key name you wish. |
| 40 | + |
| 41 | +### Optional arguments |
| 42 | + |
| 43 | +The `FT.SUGADD` command can take two optional arguments: |
| 44 | + |
| 45 | +* `INCR`: increments 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. |
| 46 | +* `PAYLOAD`: saves a string with the suggestion, which can be fetched by adding the `WITHPAYLOADS` argument to `FT.SUGGET`. |
| 47 | + |
| 48 | +## Retrieve suggestions |
| 49 | + |
| 50 | +To get autocomplete suggestions for a given prefix, use the [`FT.SUGGET`]({{< baseurl >}}/commands/ft.sugget) command. |
| 51 | + |
| 52 | +``` |
| 53 | +redis> FT.SUGGET autocomplete "he" |
| 54 | +1) "hero" |
| 55 | +2) "help me" |
| 56 | +3) "hello world" |
| 57 | +4) "hello there" |
| 58 | +``` |
| 59 | + |
| 60 | +If you wish to see the scores, use the `WITHSCORES` option: |
| 61 | + |
| 62 | +``` |
| 63 | +redis> FT.SUGGET autocomplete "he" WITHSCORES |
| 64 | +1) "hero" |
| 65 | +2) "40.414520263671875" |
| 66 | +3) "help me" |
| 67 | +4) "32.65986251831055" |
| 68 | +5) "hello world" |
| 69 | +6) "31.62277603149414" |
| 70 | +7) "hello there" |
| 71 | +8) "28.460498809814453" |
| 72 | +``` |
| 73 | + |
| 74 | +### Enable fuzzy matching |
| 75 | + |
| 76 | +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](https://en.wikipedia.org/wiki/Levenshtein_distance) of 1 from the provided prefix. |
| 77 | + |
| 78 | +``` |
| 79 | +redis> FT.SUGGET autocomplete hell FUZZY |
| 80 | +1) "hello world" |
| 81 | +2) "hello there" |
| 82 | +3) "help me" |
| 83 | +``` |
| 84 | + |
| 85 | +### Optional arguments |
| 86 | + |
| 87 | +There are three additional arguments you can use wit `FT.SUGGET`: |
| 88 | + |
| 89 | +* `MAX num`: limits the results to a maximum of `num`. The default for `MAX` is 5. |
| 90 | +* `WITHSCORES`: returns the score of each suggestion. |
| 91 | +* `WITHPAYLOADS`: returns optional payloads saved with the suggestions. If no payload is present for an entry, a `nil` reply is returned. |
| 92 | + ``` |
| 93 | + redis> FT.SUGADD autocomplete hero 70 PAYLOAD "you're no hero" |
| 94 | + (integer) 4 |
| 95 | + redis> FT.SUGGET autocomplete "hr" FUZZY WITHPAYLOADS |
| 96 | + 1) "hero" |
| 97 | + 2) "you're no hero" |
| 98 | + 3) "help me" |
| 99 | + 4) (nil) |
| 100 | + 5) "hello world" |
| 101 | + 6) (nil) |
| 102 | + 7) "hello there" |
| 103 | + 8) (nil) |
| 104 | + ``` |
| 105 | +
|
| 106 | +## Delete suggestions |
| 107 | +
|
| 108 | +To remove a specific suggestion from the dictionary, use the `FT.SUGDEL` command. |
| 109 | +
|
| 110 | +``` |
| 111 | +redis> FT.SUGDEL autocomplete "help me" |
| 112 | +(integer 1) |
| 113 | +``` |
| 114 | +
|
| 115 | +After deletion, running `FT.SUGGET autocomplete hell FUZZY` will no longer return "help me". |
| 116 | +
|
| 117 | +## Check the number of suggestions |
| 118 | +
|
| 119 | +To get a count of the number of entries in a given suggestions dictionary, use the `FT.SUGLEN` command. |
| 120 | +
|
| 121 | +``` |
| 122 | +redis> FT.SUGLEN autocomplete |
| 123 | +(integer) 3 |
| 124 | +``` |
| 125 | +
|
| 126 | +## Use autocomplete with search |
| 127 | +
|
| 128 | +A common approach is to: |
| 129 | +
|
| 130 | +1. Use FT.SUGGET to suggest query completions as users type in a text field. |
| 131 | +1. Once the user selects a suggestion, run FT.SEARCH using the selected term to get full search results. |
| 132 | +
|
| 133 | +Example workflow |
| 134 | +
|
| 135 | +1. Get suggestions for a given user input. |
| 136 | +
|
| 137 | + ``` |
| 138 | + FT.SUGGET autocomplete "hel" |
| 139 | + ``` |
| 140 | +1. Capture the user's selection. |
| 141 | +1. Use the selected suggestion in a full-text search. |
| 142 | +
|
| 143 | + ``` |
| 144 | + FT.SEARCH index "hello world" |
| 145 | + ``` |
| 146 | +
|
| 147 | +### When to use autocomplete versus full-text search |
| 148 | +
|
| 149 | +* Use `FT.SUGGET` when you need fast, real-time prefix-based suggestion retrieval. |
| 150 | +* Use `FT.SEARCH` when you need document retrieval, filtering, and ranking based on relevance. |
| 151 | +
|
| 152 | +## Autocomplete use cases |
| 153 | +
|
| 154 | +The autocomplete feature in Redis Query Engine is useful for: |
| 155 | +
|
| 156 | +- **Search box suggestions**: providing live suggestions as users type. |
| 157 | +- **Command completion**: offering autocompletion for CLI tools. |
| 158 | +- **Product search**: suggesting product names in e-commerce applications. |
| 159 | +- **Chatbot responses**: recommending common phrases dynamically. |
0 commit comments