#399 Autocomplete Search Terms pro
Dec 31, 2012 | 17 minutes | Performance, Caching
Learn how to add autocompletion to a search form and improve performance using Rack middleware, caching and switching from SQL to Redis.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Typo in rack middleware?
appication/json
What a great episode! I really like that you use Redis. I'd be grateful to see more episodes about Redis and Rails.
isn´t there a typo in
[200, {"Content-Type" => "appication/json"}, [terms.to_json]]
shouldn´t it be "application/json"
and how come it works anyway?
I believe jQuery is clever enough to work out that it's JSON anyway.
It does break nginx to return the wrong header, so it's definitely worth fixing.
How do we go about having redis automatically update caching (development and production) if new products are being added on a regular basis without running
rake search_suggestions:index
each time a new record is being added in the database?!!
you would want to use a scheduler of some sort that calls the rake task every fixed interval of time. check out the Whenever gem, or if you're deploying on Heroku there's an easy to use scheduler add-on.
Yes i am deploying to Heroku. I will check it out. Thanks for the info.
The way soulmate does this is to add a
after_save
hook to load the term in to redis.+1
I get the error "can not update on a new record object" for the method index_term on the increment!. This makes sense but I am not sure of the best way to fix it other than using first_or_create. Suggestions(hehe)?
Did you find a solution to this? I'm having the same issue.
I am using Soulmate in production, and I am facing weird performance issues. The Soulmate sinatra app doesn't respond to some queries at all, whereas for other queries the performance is pretty good.
Anyways, if you decide to use Soulmate, you should go through http://patshaughnessy.net/2011/11/23/finding-your-soulmate-autocomplete-with-redis-in-rails-3-1 and Salvatore's original post on doing autocomplete with redis - http://oldblog.antirez.com/post/autocomplete-with-redis.html
@Ryan It will be great if you can add these to the show notes.
Thanks Ryan for another excellent Railscast. I have seen several screencasts on the subject of search, and all of them limit the 'domain' of the search to just one 'entity'. This screencast searches Products. I need a search that finds anything on the, whether it be Products, or BlogPosts, or Orders or whatever. I can imagine one way to accomplish this - simply duplicate the effort here for Products, adding Orders alongside it, and then in the search results present a section for Products Found and another section for Orders Found. But isn't there a better way to accomplish this?
I've done this using Soulmate before. I added the after_save for the models I wanted to be able to search for. In my data I'd store the search term as the key, and in the value i'd store a hash containing the full name, id, and type. Worked great.
I ran into an issue when using the ActiveRecord version of this... Specifically, after starting up the server and entering a bunch of search requests (and then doing a bunch of other things on the site,) I began to get a warning in the log complaining about my db connection not being closed properly. This error coincided with every search after that point in time.
I think this may be because the middleware is inserted prior to ActiveRecord::ConnectionAdapters::ConnectionManagement (which is responsible for opening and closing DB connections.) ???
So, I inserted the middleware after the ConnectionManagement class and it seems to be working now.
Hi Ryan (and all),
I'm getting times near to 10ms when retrieving search suggestions (using redis). BTW, my dev enviroment is a Mac Book Pro (8GB Ram, intel core I7)
Any clues on why I'm getting such bad response times? Based on the video I guess they should be better.
Why not use Postgres' full-text search @@ operator rather than LIKE?
Ok, I have it almost working (without redis), but my question is that whenever I get a dropdown from this, if I put my mouse over the results or I press down arrow, the results go away. How can I get it to be able to select one of the results and fill in the rest of the search box?
Hi Ryan...This tutorial is really AWESOME..I'm just a newbie in rails but I never had a hard time understanding your tutorials...I have a question, Can I automate the rake task that index the search suggestions by event or by time?..Thanks in advance..
Hi, how to hide terms that are not present currently in index(deleted from models)?
To learn how to implement autocomplete using Twitter typeahead javascript, elasticsearch with searchkick in Rails 5, check out Autocomplete using Typeahead and Searchkick in Rails 5
Good job and bunch of thanks for this valuable post.
I do not think it will work this way
Well try this geekspie