Elasticquery
A module for elasticquery ruby libraries for using user-friendly query generator. Click here to view demo with code examples.
Instalation
To install using Bundler grab the latest stable version:
gem 'elasticquery'
To manually install elasticquery via Rubygems simply gem install:
gem install elasticquery
Getting Started
First instruction
Elasticsearch was designed to be customized as you need to. Providing simple methods it allows you to build power and flexible form objects. For example:
class MyQuery < Elasticquery::Base
filtered do |params|
search params[:query]
term user_id: params[:user_id] if params[:user_id].present?
range.not :age, gte: 18
end
end
Then use it
query = MyQuery.new query: 'i typed', user_id: 5
query.build # => query for elasticsearch
Article.search query.build # => be happy
Currently you have
Note! After first releases of elasticuqery it has scarce support of options and methods. Pull requests and issues with your ideas are welcome!
Extended instruction
There are multiple ways to organize your query, using chaining calls, or custom filters. Better custom filters support in progress now.
- Chain calls
ruby PeopleQuery.new.search('hi', operator: :or).term(age: 21).build # => es-ready query - Class methods ```ruby class PeopleQuery < Elasticquery::Base filtered do |params| range :age, lte: prepare_age(params[:max_age]) end
protected
def prepare_age(param) param.to_i end end PeopleQuery.build(max_age: '42') # => es-ready
- Multiple `filtered` blocks
```ruby
class ChildQuery < Elasticquery::Base
filtered do |params|
term :'category.id' => params[:category_id]
end
filtered do |params|
term :'author.name' => User.find(params[:user_id]).name
end
end
ChildQuery.build({user_id: 1, category_id: 14}) => # ;)
- Query inheritance ```ruby class ParentQuery < Elasticquery::Base filtered do |params| term :'category.id' => params[:category_id] end end
class ChildQuery < ParentQuery filtered do |params| term :'author.name' => User.find(params[:user_id]).name end end
ChildQuery.build(1, category_id: 14) => # the same as in previous example
- Custom filter methods
```ruby
in progress...
- Custom filter classes
ruby in progress...
Usage
term
# Simple one term filter
term category: 'Rock'
# Multiple filters joined by AND condition
term category: 'Soul', user: 'Aaron'
# Term exclusion
term.not category: 'Rap'
search (multimatch)
# Simple all fields search in your index
search 'developers'
# The same as above
search 'developers', fields: "_all", operator: "and", type: "best_fields"
# Configure fields
search 'Jordan', fields: ['first_name', 'last_name'], operator: "or"
range
# One side range
range :age, gte: 18
# Double sides range
range :volume, gte: 1, lte: 100
# Range exclusion
range.not :size, gte: 32, lte: 128
Contributing
- I'm happy to see any method you can be part of this.