Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:49:34+00:00 2026-05-13T08:49:34+00:00

How can I implement an autocomplete using redis? Say for example I have an

  • 0

How can I implement an autocomplete using redis?

Say for example I have an array ["alfred","joel","jeff","addick"]. When I type a I get ["alfred", "addick"]

I hope you get the point. How can I implement this using redis commands efficiently(if possible but I think it is). It would be great if I could get some simple commands I can try out via telnet to mimic this behaviour.

Thanks

P.S: Merry x-mas to all of you 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:49:34+00:00Added an answer on May 13, 2026 at 8:49 am

    If you’re dealing with a large data set, I would suggest considering implementing this as a trie. I have thrown together a small bit of Ruby that would do this:

        require 'rubygems'
        require 'redis'
        
        class RedisTrie
          TERMINAL = '+'
        
          def initialize(prefix)
            @prefix = prefix
            @r = Redis.new
          end
        
          def add_word(word)
            w = word.gsub(/[^a-zA-Z0-9_-]/, '')
            key = "#{@prefix}:"
        
            w.each_char do |c|
              @r.zset_add key, c.bytes.first, c
              key += c
            end
        
            @r.zset_add key, 0, TERMINAL
          end
        
          def add_words(*words)
            words.flatten.compact.each {|word| add_word word}
          end
        
          def suggest(text)
            @r.zset_range("#{@prefix}:#{text}", 0, -1).map do |c|
              (c == TERMINAL) ? text : suggest(text + c)
            end.flatten
          end
        end
        
        rt = RedisTrie.new('trie')
        
        rt.add_words %w( apple automobile carwash oil-change cranky five ruthie axe auto )
        
        p rt.suggest(ARGV.shift.to_s)
    

    For example:

        $ ruby RedisTrie.rb
        ["apple", "auto", "automobile", "axe", "carwash", "cranky", "five", "oil-change", "ruthie"]
        $ ruby RedisTrie.rb a
        ["apple", "auto", "automobile", "axe"]
        $ ruby RedisTrie.rb au
        ["auto", "automobile"]
        $ ruby RedisTrie.rb aux
        []
    

    Read more on Tries at Wikipedia’s entry on Tries.

    You will definitely want to optimize your suggest method to not return ALL values, instead only returning the first X values it finds. It would defeat the purpose to iterate the entire data structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 293k
  • Answers 293k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The error messages are hinting that components are missing or… May 13, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer Rack Middleware After doing a lot of investigation after I'd… May 13, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer I'm assuming that you have two models - Artist and… May 13, 2026 at 6:25 pm

Related Questions

I'd like to create an autocomplete control using Boo/C#. Here's a screenshot from the
Using ASP.NET MVC, I've implemented an autocomplete textbox using the approach very similar to
I'm trying to use the official jQuery autocomplete plugin with an ASMX web service
How can I implement an enumeration type (spelled enum in some languages) in Python?
I want to implement an index that is being displayed at the right side

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.