Project

arelastic

0.02
Low commit activity in last 3 years
No release in over a year
Build Elastic Search queries with objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Build Status Code Climate

Arelastic

Arelastic is a ElasticSearch AST manager for Ruby. It simplifies the generation complex of Elasticsearch queries.

It was extracted from my Elastic Record project.

Usage

Search

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 & Offset

# Limit
Arelastic::Searches::Size.new(20).as_elastic
# => {"size"=>20}

# Offset
Arelastic::Searches::From.new(20).as_elastic
# => {"from"=>20}

Ordering

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"}]}

Putting It All Together

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.