Search kick Gem is a Ruby gem that runs on top of Elasticsearch and makes it easy to make searches in a Rails-friendly fashion. In addition, it allows you to add more features including analytics, autocomplete, and personalized results.
Searchkick realizes what your users are searching for. As more individuals hunt, it gets more brilliant and the outcomes improve. It’s benevolent for designers – and supernatural for your users. It handles stemming, special characters, extra whitespace, misspellings, custom synonyms.
To get started, make sure that you have Elasticsearch installed on your home computer. Depending on your operating system, the installation process is slightly different and make sure you have at least Java 7.
Once you have that done, add and install searchick to your Rails application by adding the following to your Gemfile and running bundle install.
gem ‘searchkick’When you have both installed and ready to go, you need to indicate which models you might want to be able to search through in your application. Just add searchkick to the model file to make it work. Then, we need to reindex the models so that Elasticsearch can run properly. In your terminal, run:
rake searchkick:reindex:allSeeking with searchkick is entirely straightforward. Basically run YourModel.search, trailed by the parameters of the search and any filters that you want to add on. For instance, one of more complex searches is below: @offers = Offer.search params[:search], page: params[:page], per_page: 10, order: {starttime: :desc}, fields: [{offer_name: :word_start}, {offer_request_name: :word_start}:price], where: { starttime: { gte: DateTime.strptime(params[:fromdate], ‘%m/%d/%Y’), lte: DateTime.strptime(params[:todate], ‘%m/%d/%Y’) } } In this search, we take the search query of the user with params[:search], and look through of the lessons with the following conditions: Pagination: This portion of code returns the results of the search into pages of 10. You can alter what number of list items you need on every page using this method. This works with both will_paginate and kaminari. Order: order: {starttime: :desc}- This method determines what order the results are returned in. By using this we can change the result display with ascending order or descending order. In this case, we returned the most recent results first. Fields: [{offer_name: :word_start},{offer_request_name: :word_start} :price]- In this method, we allowed users to search by offer name, offer type, or the offer price. The word_start method allows for users to only type in the first part of the offer name and still get the offer in search results. For example, a search of “ch” would still reveal “chat”. You can also use word_middle and word_end. For longer text, searchkick also supports text_start, text_middle, and text_end. Where: starttime: {gte: DateTime.strptime(params[:fromdate], ‘%m/%d/%Y’), lte: DateTime.strptime(params[:todate], ‘%m/%d/%Y’)}. It allows users to easily use filters in their application. We wanted to filter between dates we used starttime to find the offers Setting Custom Search Attributes One last great feature in searchkick is the search_data method that you can add to your models. This allows you to add or replace attributes that the user can search for. By doing this, you can create custom search parameters or search based on the associations of a model. Below is an example of one of the search_data methods we implemented. def search_data attributes.merge( offer_request_name: offer_requests.map(&:name), ) end Multiple Indices Search across multiple indices with:
Searchkick.search “search_query”, index_name: [Offer, OfferRequest]Our Ruby on Rails developers here at RailsCarma don’t just create applications but they create phenomenon. Making use of the futuristic techniques, they deliver brilliant applications, design beautiful and engaging UI/UX, develop clear front ends, build scalable applications & manage them on Cloud. In short, our Ruby on Rails developers – “Give Life to Your Ideas”. Comment below or Contact Us in case of any query.
Katneni Naga Sai Tejaswi
Sr. Software Developer
Subscribe For Latest Updates
Related Posts