Arelastic is a ElasticSearch AST manager for Ruby. It simplifies the generation complex of Elasticsearch queries.
It was extracted from my Elastic Record project.
search = Arelastic::Builders::Search['name']
# Name equals red
search.eq('red').as_elastic
# => {"term"=>{"name"=>"red"}}
# Negation
search.eq("red").negate.as_elastic
# => {"bool"=>{"must_not"=>{"term"=>{:name=>"red"}}}}
# Limit
Arelastic::Searches::Size.new(20).as_elastic
# => {"size"=>20}
# Offset
Arelastic::Searches::From.new(20).as_elastic
# => {"from"=>20}
sort_field = Arelastic::Sorts::Field.new('price' => 'asc')
sort_field.as_elastic
# => {'price' => 'asc'}
sort = Arelastic::Searches::Sort.new([sort_field])
sort.as_elastic
# => {"sort"=>[{"price"=>"asc"}]}
search = [
Arelastic::Searches::Query.new(Arelastic::Builders::Search['name'].eq('Fun')),
Arelastic::Searches::Size.new(20),
Arelastic::Searches::From.new(20),
Arelastic::Searches::Sort.new([Arelastic::Sorts::Field.new('price' => 'asc')])
]
Arelastic::Nodes::HashGroup.new(search).as_elastic
# => {"query"=>{"term"=>{"name"=>"Fun"}}, "size"=>20, "from"=>20, "sort"=>[{"price"=>"asc"}]}
Some helpful Arel builders can be found here.