Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (46 sloc) 1.71 KB

Build Status Code Climate

Arelastic

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

It is intended to be a framework framework; that is, you can build your own ORM with it.

One example is Elastic Record

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.

You can’t perform that action at this time.