Class: Elasticquery::Query
- Inherits:
-
Object
- Object
- Elasticquery::Query
- Defined in:
- lib/elasticquery/query.rb
Constant Summary collapse
- DEFAULT =
{query: {filtered: {query: {match_all:{}}}}}
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
Instance Method Summary collapse
-
#<<(filter) ⇒ Object
Merge filter to query.
-
#initialize(query = DEFAULT) ⇒ Query
constructor
Create new query from hash.
-
#match_all? ⇒ Boolean
Ckeck current object to default elasticsearch param.
-
#to_hash ⇒ Hash
Convery query object ot hash.
Constructor Details
#initialize(query = DEFAULT) ⇒ Query
Create new query from hash.
16 17 18 19 |
# File 'lib/elasticquery/query.rb', line 16 def initialize(query = DEFAULT) @query = query @filters = [] end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
6 7 8 |
# File 'lib/elasticquery/query.rb', line 6 def filters @filters end |
Instance Method Details
#<<(filter) ⇒ Object
Merge filter to query. If current query is ‘matche_all`ed then clear it and use new value. Populate #filters array with given classes
44 45 46 47 48 |
# File 'lib/elasticquery/query.rb', line 44 def <<(filter) @query = {} if match_all? merge filter @filters << filter end |
#match_all? ⇒ Boolean
Ckeck current object to default elasticsearch param
53 54 55 |
# File 'lib/elasticquery/query.rb', line 53 def match_all? @query == DEFAULT end |
#to_hash ⇒ Hash
Convery query object ot hash
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/elasticquery/query.rb', line 24 def to_hash q = @query.dup if Enumerable === _filters flatted_filters = _filters.flat_map do |type, filters| filters.flat_map do |key, value| {type => {key => value}} end end q[:query][:filtered][:filter][:and] = flatted_filters end q end |