ElasticRansack provides searching to your elasticsearch models like Ransack or MetaSearch gems.
Your just create search form with name_cont or created_at_gt fields and ElasticRansack build a search query for you.
It compatible with most of the Ransack predicates and helpers.
ElasticRansack uses Tire and Elasticsearch for searching.
Inspired by Ransack gem.
Add this line to your application's Gemfile:
gem 'elastic_ransack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install elastic_ransack
Include ElasticRansack extension to your model:
class User < ActiveRecord::Base
include ElasticRansack::Model
endElasticRansack provides #elastic_ransack method for searching:
User.elastic_ransack(name_cont: 'alex', role_id_eq: 1,
state_id_in: [2, 3],
created_at_gt: 1.day.ago)It return Tire::Results::Collection instance like Tire gem.
To return ActiveRecord objects you should add :load option:
User.elastic_ransack({created_at_gt: 1.day.ago}, load: true)To paginate your results:
User.elastic_ransack({created_at_gt: 1.day.ago}, page: 2, per_page: 30):ssorting column, can contain sort mode separated by space, example:id desc:q_contsearch against _all fields
_contcontains string value_eqequal value_ininclude any of the values_in_allinclude all values_gtgreater then value_ltless then value_gteqgreater or equal the value_lteqless or equal the value_not_eqnot equal value_presentnon-null value, mapped to exists filter
Product.elastic_ransack(name_cont: 'alex', category_id_eq: 1, tag_ids_in: [2, 3])
Product.elastic_ransack(tag_ids_in: '2,3,4')
Product.elastic_ransack(created_at_gt: 1.day.ago)
Product.elastic_ransack(q_cont: 'table')
Product.elastic_ransack(s: 'price desc')For search on localized attributes like name_en use translations_ prefixed field:
Product.elastic_ransack({translations_name_cont: 'chair'}, globalize: true)It will search on name_en field (depending on current locale)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request

