|
| 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 search and suggestions. |
| 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 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 suggestions with [`FT.SEARCH`]({{< baseurl >}}/commands/ft.search). |
| 24 | + |
| 25 | +## Add autocomplete suggestions |
| 26 | + |
| 27 | +To add phrases or words–suggestions–to an autocomplete dictionary, use [`FT.SUGADD`]({{< baseurl >}}/commands/ft.sugadd). You will assign a score to each entry, which determines its ranking in the results (higher scores rank first). |
| 28 | + |
| 29 | +``` |
| 30 | +FT.SUGADD autocomplete "hello world" 100 |
| 31 | +FT.SUGADD autocomplete "hello there" 90 |
| 32 | +FT.SUGADD autocomplete "help me" 80 |
| 33 | +FT.SUGADD autocomplete "hero" 70 |
| 34 | +``` |
| 35 | + |
| 36 | +The use of "autocomplete" in the above examples is just the name of the key. You can use any key name you wish. |
| 37 | + |
| 38 | +## Retrieve suggestions |
| 39 | + |
| 40 | +To get autocomplete suggestions for a given prefix, use [`FT.SUGGET`]({{< baseurl >}}/commands/ft.sugget): |
| 41 | + |
| 42 | +``` |
| 43 | +redis> FT.SUGGET autocomplete "he" |
| 44 | +1) "hero" |
| 45 | +2) "help me" |
| 46 | +3) "hello world" |
| 47 | +4) "hello there" |
| 48 | +``` |
| 49 | + |
| 50 | +If you wish to see the scores, use the `WITHSCORES` option: |
| 51 | + |
| 52 | +``` |
| 53 | +redis> FT.SUGGET autocomplete "he" WITHSCORES |
| 54 | +1) "hero" |
| 55 | +2) "40.414520263671875" |
| 56 | +3) "help me" |
| 57 | +4) "32.65986251831055" |
| 58 | +5) "hello world" |
| 59 | +6) "31.62277603149414" |
| 60 | +7) "hello there" |
| 61 | +8) "28.460498809814453" |
| 62 | +``` |
| 63 | + |
| 64 | +The displayed scores will differ from what you used when you added the suggestions. This is because scores are normalized internally. |
| 65 | + |
| 66 | +### Enabling Fuzzy Matching |
| 67 | + |
| 68 | +If you want to allow small spelling mistakes or typos, use the `FUZZY` option: |
| 69 | + |
| 70 | +```sh |
| 71 | +FT.SUGGET autocomplete "hr" FUZZY |
| 72 | +``` |
| 73 | + |
| 74 | +**Response:** |
| 75 | +```sh |
| 76 | +1) "hero" |
| 77 | +``` |
| 78 | + |
| 79 | +## Deleting a Suggestion |
| 80 | + |
| 81 | +To remove a specific suggestion from the dictionary: |
| 82 | + |
| 83 | +```sh |
| 84 | +FT.SUGDEL autocomplete "hero" |
| 85 | +``` |
| 86 | + |
| 87 | +After deletion, running `FT.SUGGET autocomplete "he"` will no longer return "hero." |
| 88 | + |
| 89 | +## Checking the Number of Suggestions |
| 90 | + |
| 91 | +To count the number of entries in the autocomplete dictionary: |
| 92 | + |
| 93 | +```sh |
| 94 | +FT.SUGLEN autocomplete |
| 95 | +``` |
| 96 | + |
| 97 | +## Use Cases |
| 98 | + |
| 99 | +The autocomplete feature in Redis Query Engine is useful for: |
| 100 | + |
| 101 | +- **Search box suggestions**: Providing live suggestions as users type. |
| 102 | +- **Command completion**: Offering auto-completion for CLI tools. |
| 103 | +- **Product search**: Suggesting product names in e-commerce applications. |
| 104 | +- **Chatbot responses**: Recommending common phrases dynamically. |
| 105 | + |
| 106 | +## Summary |
| 107 | + |
| 108 | +The autocomplete feature of Redis Query Engine provides a fast and efficient way to implement prefix-based suggestions using a trie-based data structure. By using `FT.SUGADD`, `FT.SUGGET`, `FT.SUGDEL`, and `FT.SUGLEN`, you can build dynamic and ranked suggestion lists tailored to your application's needs. |
| 109 | + |
0 commit comments