Setting up auto-complete with Redisearch

Setting up auto-complete with Redisearch

Daily short news for you
  • Manus has officially opened its doors to all users. For those who don't know, this is a reporting tool (making waves) similar to OpenAI's Deep Research. Each day, you get 300 free Credits for research. Each research session consumes Credits depending on the complexity of the request. Oh, and they seem to have a program giving away free Credits. I personally saw 2000 when I logged in.

    I tried it out and compared it with the same command I used before on Deep Research, and the content was completely different. Manus reports more like writing essays compared to OpenAI, which uses bullet points and tables.

    Oh, after signing up, you have to enter your phone number for verification; if there's an error, just wait until the next day and try again.

    » Read more
  • I just found a quite interesting website talking about the memorable milestones in the history of the global Internet: Internet Artifacts

    Just from 1977 - when the Internet was still in the lab - look how much the Internet has developed now 🫣

    » Read more
  • Just thinking that a server "hiding" behind Cloudflare is safe, but that’s not necessarily true; nothing is absolutely safe in this Internet world. I invite you to read the article CloudFlair: Bypassing Cloudflare using Internet-wide scan data to see how the author discovered the IP address of the server that used Cloudflare.

    It's quite impressive, really; no matter what, there will always be those who strive for security and, conversely, those who specialize in exploiting vulnerabilities and... blogging 🤓

    » Read more

The problem

Search is one of the key features on any website. Through search functionality, users can easily explore the content of your website.

There are many ways to provide search capabilities to users. For example, categorizing options for them to choose from, or using tags to filter content, or simply providing a search box for them to freely enter their own search queries.

Nowadays, user experience is becoming increasingly important, and it is the responsibility of website administrators to reduce any hesitation or confusion users may have when using certain features on a website, as this can increase the bounce rate. If you have implemented search functionality, what can you do to make it more useful for users?

Let's take the example of a website that sells various fashion accessories, and one of the trending products is "fashion watches" or "superhero watches". How can you enable the user to simply enter "watch" and immediately see suggested keywords that you have set up in advance?

Auto-completion

Auto-completion

Auto-completion is a feature provided by Redisearch for search suggestion. The way auto-completion works is quite simple. You create a separate index specifically for suggesting relevant phrases based on the search keyword.

Redisearch also supports fuzzy suggestions, meaning you can still receive results for a keyword even if the user makes a spelling mistake. This is achieved by using Levenshtein Automaton, which enables efficient search within the Levenshtein Distance. The suggestions are then weighted based on both their score and their Levenshtein distance from the user-entered keyword.

However, fuzzy search (especially for short prefixes) can result in a large number of suggestions. In fact, fuzzy search for any single letter will iterate through the entire dictionary, so this feature should be used carefully as it can impact search speed and server resources.

Setup

All we need to do is create indexes specifically for the suggestion feature and define scores to prioritize the display of search results.

For example, in my blog I have articles related to the topic of Node.js, such as "What is Node.js", "Node.js Event Loop", "Learn Node.js", and I want to suggest these phrases when the user enters "node.js".

127.0.0.1:6379> FT.SUGADD article "What is Node.js" 100
(integer) 1

127.0.0.1:6379> FT.SUGADD article "Node.js Event Loop" 200
(integer) 2

127.0.0.1:6379> FT.SUGADD article "Learn Node.js" 300
(integer) 3

Then let's try searching for suggestions:

127.0.0.1:6379> FT.SUGGET article "node.js" MAX 5 WITHSCORES
Learn Node.js
106.06601715087891
Node.js Event Loop
57.735027313232422
What is Node.js
37.79644775390625

MAX 5 is to fetch the first 5 results, and WITHSCORES is used to display scores. The higher the score, the higher the priority. The order of the results above is determined by setting the score of "Learn Node.js" to the highest (300), then decreasing scores for the remaining phrases.

Suggestions also support searching for misspelled characters. For example, "nodejs", "nopejs", "nope.js", thanks to the Levenshtein distance calculation algorithm. However, along with that comes a decrease in performance. To apply this, simply add the FUZZY keyword in the query.

127.0.0.1:6379> FT.SUGGET article "nope.js" FUZZY MAX 5 WITHSCORES
Learn Node.js
106.06601715087891
Node.js Event Loop
57.735027313232422
What is Node.js
37.79644775390625

Currently, auto-completion only supports prefix keywords, which means it can suggest keywords only if the input keyword matches the beginnings of phrases. In the example above, Redis can only suggest when searching for "no", "node", "node.js", but it cannot suggest for words like "event", "is". Hopefully, future updates to auto-completion will support searching at any position within phrases.

To further explore, you can refer to Redis Auto-completion.

Conclusion

Search suggestion is a useful feature that many websites are using. By utilizing this feature, you can enhance the user experience by suggesting keywords that users are interested in, leading to more focused and relevant searches, such as "hot trends" or the main content of your website, which you want users to discover and pay attention to.

Redisearch's auto-completion provides even more benefits based on your creativity. For example, automating the search index to continuously update suggested phrases based on the search data collected from users to create "hot search trends".

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Thành Đỗ2 years ago
Nghĩa là mình vẫn phải tạo một index rồi thêm data tìm kiếm vào chứ nó không dựa theo data sẵn có được à bạn
Reply
Avatar
Xuân Hoài Tống2 years ago
Đúng rồi bạn ơi, tính năng này chỉ hỗ trợ tạo data trong index để hỗ trợ suggest thôi