Ask A Question

Notifications

You’re not receiving notifications from this thread.

Advanced Search, Autocomplete and Suggestions with ElasticSearch and the Searchkick gem Discussion

Would you still recommend using Searchkick over alternatives like elasticsearch-rails and elasticsearch-ruby?

Reply

Absolutely. If you've looked into ES-rails or ES-ruby gems there docs are pretty poor at best last I saw. I'm sure it's getting better and eventually will be on par with Searchkick, but for now they feel behind. Searchkick is basically a nice ruby-ish wrapper so everything feels intuitive whereas ES-rails and stuff came from non-ruby devs you can tell so it's less ruby-ish in style. I still use Searchkick on a daily basis and recommend it.

My guess is over the next year or so the official ES-rails/ruby gems will get all the nice helpers and things to make it smooth to use like Searchkick but until then you can't go wrong with Searchkick.

Reply

I'm a new subscriber and new to rails in general and I feel I'm in rails heaven! :)

Reply

Glad you love it and we're happy to have you as part of the community! :D

Reply

When I use ["Test"] as my rendered Json it works, however when I try to use data from my posts table, it no longer works. I have rechecked syntax and spelling multiple times and am 100% sure its right exactly following this tutorial. Any Ideas? I've posted in stack overflow, no response, attempted dozens of tutorials with different implementation methods, no use. It's really starting to annoy me because it should be quick and easy like your video. I payed and subscribed to your website hoping you could help me. I also posted this question with my code in the communities section.

Reply

Hi Chris, I have the following question about installing and running es in production:

In this article, the following is mentioned:
https://www.digitalocean.co...

"This tutorial assumes that your servers are using a VPN like the one described here: How To Use Ansible and Tinc VPN to Secure Your Server Infrastructure."

In this article however there is no mention of the need for a vpn and Tinc:

https://www.digitalocean.co...

This is a bit confusing, is this vpn necessary for basic usage of elasticsearch or not, or private networking only? It seems like a lot of work to set this all up. Or is the approach in the second article sufficient? ( authentication + firewall)

Thanks
Thomas

Reply

Great questions. I've only used Elasticsearch in production on Heroku where they take care of the security for you. As for securing your own instance, I don't believe that you need a VPN in order to secure it properly, but you will definitely want the authentication and firewall. There are some other suggestions out there about adding in SSL and some other things as well, for example: https://brudtkuhl.com/secur...

Reply

Thanks Chris. Questions:

1. Does Searchkick auto reindex questions after new question is created?

2. If number 1 is no, what's the best way to make it happen? A callback inside the Question model?

3. You did mentioned earlier inside the video where Searchkick has a 'smart feature' where it can rank the result based on the popularity. Does it available out of the box or extra configuration required?

Thanks.

Reply

1. It will auto index the new question, but only the new one that is added. It won't touch the existing ones. Searchkick implements callbacks on the models to automatically index them when they change.

3. You'd have to configure this. For example, you have to figure out what "popular" means. Is it views? Comments? etc. Once you figure out what you want, you can then pass in that data into your search_data so Searchkick indexes it and then you can query against it.

Reply

Hi Chris, I want to run Elasticsearch on a separate server. I can't seem to connect to it. I can curl it locally fine but not externally. I tried opening the ports and still no go. I suspect I'm not configuring the elasticsearch.yml correctly... Any tips?

Reply

These are likely the two main ones that could be misconfigured:

1. Make sure elasticsearch is bound to the public IP address and not just running internally.
2. Check to make sure the port is open on it through the firewall.

It's probably one of these two but could be something else. It's been a while since I setup ES across machines.

Reply
Jesse Krasnostein Jesse Krasnostein

Really enjoyed this video and found it super helpful. One question I have relates to autocomplete. We used
fields: [{name: :text_start}, limit: 10]).map(&:name)
to specify where we'd like to get the autocomplete data from, but what if we wanted to do this on two fields of an entity?

As an example, say you have Person 1 (name = John) and Person 2 (name = Rachel)
Say they both have own a dog that is a breed of "Labrador"... I want my autocomplete to work when I start typing there names or if I start typing labr.... it word return labrador as an option in the autocomplete.
I was thinking it could be something like fields: [{name: :text_start, breed: :text_start}, limit: 10]).map(&:name), but then the map part makes no sense...

Reply

I think you're close, but you want an array of hashes.

Person.search params[:q], {
fields: [{name: :text_start}, {breed: :text_start}],
limit: 10
}

The searchkick docs also show another method, putting the autocomplete types on the searchkick method in the model and just listing out the fields by name. https://github.com/ankane/s...

Reply

Hi Chris. Watching the episode, I was wondering how i could implement this feature using a single search input box which is part of my navbar.

Indeed, my app goes through different index views (switchable through sidebar items) but the search input stays at the top. My workflow would be to perform individual searches based on the index view i am and not search across all models. Shall I hide show identical search boxes based on the view page?

I think there are ways to be more clever and to leverage only one and redirect the search with "if statements" (model, view or controller name) but great to have people views on this.

Reply

Well, your search in the navbar implies that it's a global search, not just for the category you're viewing. You can do searches across the different models and combine the results (maybe show the current category results first). You can see an example of this on Facebook with the global search bar, yet different categories of results (pages, people, businesses, etc).

That would definitely make for a good episode and I've wanted to do this for GoRails sometime soon anyways to search both episodes and the forum.

Reply

Hi Chris. I had 2 follow up questions for you:
1. how can you send the autocomplete selection directly/automatically to the search instead of the video two steps (indeed first you select and then you have to type enter)?
2. in the autocomplete, how do you merge 2 fields. say you look at last_name but you want to display both first_name and last_name concatenated.
have a good day! Stan

Reply

1. I'm not quite sure what you're asking on this one. Rephrase and try me again? :P
2. You'd have to index them combined, so you'd make a "name" attribute that was those concatenated and then search on that instead of the separate fields.

Reply

Hey man, I can't figure out how to style the suggestions that show up beneath the search bar. Any links or ideas? I keep trying to Google this in different ways but I'm still coming up emptyhanded.

Reply

I figured my own problem out, finally. It is a <ul> and each <li> in the dropdown has the ui-menu-item class, and that's really all you need to attach the styling to, plus :hover for some feedback when the user mouses over the list items. Hope this helps someone else!

Reply

I want to sort the searchkick results with ransack.
@products = Product.search(query).ransack()

It is throwing error: NoMethodError: undefined method `ransack' for #<Searchkick::Results
Is it possible to use ransack and searchkick .
Help!

Reply

I am try to use :text_middle to autocomplete but no success. I have replaced :text_start with :text_middle the same way you have done in the above video.

Reply

autocomplete search the text, using a javascript file instead of a CoffeeScript file. can you share the code for it?

Reply
$( "#tags" ).autocomplete({
  source: availableTags
});
Reply

I have a doubt. How can we search the fields with associated models?

Reply

@Chris : great video. I am trying to implement a similar solution but my model is using Action Text for the body and I am not able to search on that field. When I try to define a data set in my model I am not able to index the content... Any help ?

Reply

I found a solution :

In the model add : searchkick word_start: [:title, :body]

def search_data
{
title: title,
rich_text_content: body,
}
end

Reply

Ran into this as well, and Charles' solution did not work for me, but just using body.to_plain_text did. I ended up with this:

searchkick word_middle: [:body]

def search_data
{
body: body.to_plain_text
}
end

Reply

Hello Chris and thanks for the video!

I've tried following your tutorial and i can't seem to make it work, the video dates a little and I'm afraid there were changes on Searchkick's part that's not reflected in the present video anymore (i.e text_start is now word_start in searchkick documentation...)

Using Rails 6 with webpacker feels a little different than what you got here, for instance when installing jquery-ui gem etc.

Any chance you would have a more recent tutorial regarding autocomplete ? With Searchkick or Anything working ?

Thanks a lot!!

Reply

I sure hope Chris will do this soon :)

Reply

I agree. I'd like to know if JQ UI is still a good choice for 2022 and how it fits into the current FE ecosystem.

Reply

Thanks for your this great video @excid3:disqus

Reply

Any chance you would have a more recent tutorial regarding autocomplete ? With Searchkick or Anything working ? https://get-mxplayer.in https://get-mxplayer.in/mx-player-for-pc-download/

Reply
Reply

Has anyone started getting this error recently -> "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone."

I'm stumped on how to fix it. Added a StackOverflow question on the topic here:

https://stackoverflow.com/questions/67993633/how-to-fix-this-in-error-rails-warning-299-elasticsearch-built-in-security-fea

Reply

This will happen with ES on your local machine bc the secutiry features actually are not enabled. You can ignore this for local dev. In prod, something like a hosted ES instance on Heroku will have default security settings. I silenced this warning locally and in CI with "xpack.security.enabled: false"

Reply

Thanks for this. My bootcamp covered this briefly, but it's great to have this extra context and info!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.