Class: EsSearchable::SearchCollection
- Inherits:
-
Object
- Object
- EsSearchable::SearchCollection
- Extended by:
- Forwardable
- Defined in:
- lib/es_searchable/search_collection.rb
Constant Summary collapse
- SearchName =
{ and: :must, or: :should, not: :must_not }
- Attrs =
[:collections, :response, :count, :time]
Instance Method Summary collapse
- #==(coll) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(klass) ⇒ SearchCollection
constructor
A new instance of SearchCollection.
- #like(params) ⇒ Object
- #limit(limit) ⇒ Object
- #load ⇒ Object
- #load_json ⇒ Object
- #map(&block) ⇒ Object
- #offset(offset) ⇒ Object
- #parse_like_params(key, value) ⇒ Object
- #search_params ⇒ Object
- #select(*attrs) ⇒ Object
Constructor Details
#initialize(klass) ⇒ SearchCollection
Returns a new instance of SearchCollection.
7 8 9 10 |
# File 'lib/es_searchable/search_collection.rb', line 7 def initialize(klass) @klass = klass @time, @response, @count, @collections = nil, nil, nil, nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object (private)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/es_searchable/search_collection.rb', line 164 def method_missing(meth, *args, &blk) case meth when /(.*)_(gt|lt|gte|lte)/ conds = [{ range: { $1 => { $2.to_sym => args.first } } }] store_conditions(:filter, :must, conds) return self.clone when /(.*)_between/ conds = [{ range: { $1 => { gte: args[0], lte: args[1] } } }] store_conditions(:filter, :must, conds) return self.clone when /(.*)_like/ if args.length == 1 conds = [{ match: { $1 => args.first } }] store_conditions(:query, :must, conds) elsif args.length == 2 && %w(and or).include?(args.last.to_s) conds = [{ match: { $1 => { operator: args.last, query: args.first } } }] store_conditions(:query, :must, conds) else end return self.clone else super end end |
Instance Method Details
#==(coll) ⇒ Object
93 94 95 |
# File 'lib/es_searchable/search_collection.rb', line 93 def ==(coll) self.search_params == coll.search_params end |
#each(&block) ⇒ Object
81 82 83 |
# File 'lib/es_searchable/search_collection.rb', line 81 def each(&block) self.load.collections.each(&block) end |
#like(params) ⇒ Object
31 32 33 34 |
# File 'lib/es_searchable/search_collection.rb', line 31 def like(params) store_conditions :query, :must, params.map { |k, v| parse_like_params(k, v) } self.clone end |
#limit(limit) ⇒ Object
49 50 51 52 |
# File 'lib/es_searchable/search_collection.rb', line 49 def limit(limit) @limit = conditions[:size] = limit self.clone end |
#load ⇒ Object
72 73 74 75 |
# File 'lib/es_searchable/search_collection.rb', line 72 def load load_data @klass.handle_es_response(self) end |
#load_json ⇒ Object
77 78 79 |
# File 'lib/es_searchable/search_collection.rb', line 77 def load_json load_data end |
#map(&block) ⇒ Object
85 86 87 |
# File 'lib/es_searchable/search_collection.rb', line 85 def map(&block) self.load.collections.map(&block) end |
#offset(offset) ⇒ Object
54 55 56 57 |
# File 'lib/es_searchable/search_collection.rb', line 54 def offset(offset) @offset = conditions[:from] = offset self.clone end |
#parse_like_params(key, value) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/es_searchable/search_collection.rb', line 36 def parse_like_params(key, value) if value.is_a?(Hash) { match: { key => value.map {|k, v| { operator: k, query: v}}.first }} else { match: { key => value }} end end |
#search_params ⇒ Object
89 90 91 |
# File 'lib/es_searchable/search_collection.rb', line 89 def search_params conditions end |
#select(*attrs) ⇒ Object
44 45 46 47 |
# File 'lib/es_searchable/search_collection.rb', line 44 def select(*attrs) conditions.merge!(fields: attrs) self.clone end |