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
end
ElasticRansack 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)
:s
sorting column, can contain sort mode separated by space, example: id desc
:q_cont
search against _all fields_cont
contains string value_eq
equal value_in
include any of the values_in_all
include all values_gt
greater then value_lt
less then value_gteq
greater or equal the value_lteq
less or equal the value_not_eq
not equal value_present
non-null value, mapped to exists filterProduct.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)
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)