ElasticRansack
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.
Installation
Add this line to your application's Gemfile:
gem 'elastic_ransack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install elastic_ransack
Usage
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)
Options
-
:s
sorting column, can contain sort mode separated by space, example:id desc
-
:q_cont
search against _all fields
Predicates
-
_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 filter
Examples
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)
Contributing
- 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