Class: Indexes::Collection
- Inherits:
-
Object
- Object
- Indexes::Collection
- Includes:
- Enumerable
- Defined in:
- lib/indexes/collection.rb
Instance Method Summary collapse
- #includes(*args) ⇒ Object
-
#initialize(index, args = [], options = {}, &block) ⇒ Collection
constructor
A new instance of Collection.
- #order(options) ⇒ Object
- #page(number, options = {}) ⇒ Object
- #query ⇒ Object
- #response ⇒ Object
- #with(ids) ⇒ Object
- #without(ids) ⇒ Object
Constructor Details
#initialize(index, args = [], options = {}, &block) ⇒ Collection
Returns a new instance of Collection.
11 12 13 14 15 16 17 |
# File 'lib/indexes/collection.rb', line 11 def initialize(index, args=[], ={}, &block) @loaded = false @index = index @args = args = @block = block end |
Instance Method Details
#includes(*args) ⇒ Object
27 28 29 |
# File 'lib/indexes/collection.rb', line 27 def includes(*args) chain includes: args end |
#order(options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/indexes/collection.rb', line 54 def order() mappings = Indexes.configuration.mappings values = [] .each do |property, direction| if block = Indexes.configuration.computed_sorts[property] values << Dsl::Api.new(direction, &block).to_h elsif property == :id values << { _uid: { order: direction } } elsif mappings.has_key?(property) && mappings[property][:type] == 'string' values << { "#{property}.raw" => { order: direction } } end end if values.any? chain sort: values else chain end end |
#page(number, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/indexes/collection.rb', line 31 def page(number, ={}) length = page_option(, :length, 10) padding = page_option(, :padding, 0) current_page = [number.to_i, 1].max values = Module.new do define_method :page_length do length end define_method :padding do padding end define_method :current_page do current_page end end chain( Pagination, values, from: ((length * (current_page - 1)) + padding), size: length ) end |
#query ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/indexes/collection.rb', line 82 def query @query ||= begin pagination = .slice(:from, :size, :sort) without_ids = fetch_ids([:without]) body = Dsl::Search.new(args, &block).to_h[:query] request = Dsl::Search.new do if without_ids.any? query do filtered do filter do bool do must_not do without_ids.each do |id| term do _id id end end end end end query body end end else query body end i(from size).each do |name| if pagination.has_key?(name) send name, pagination[name] end end if pagination.has_key?(:sort) sort pagination[:sort] else sort do _uid do order 'desc' end end end end request.to_h end end |
#response ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/indexes/collection.rb', line 73 def response if @loaded == true @response else @loaded = true @response = index.raw_search(query) end end |
#with(ids) ⇒ Object
19 20 21 |
# File 'lib/indexes/collection.rb', line 19 def with(ids) chain with: ids end |
#without(ids) ⇒ Object
23 24 25 |
# File 'lib/indexes/collection.rb', line 23 def without(ids) chain without: ids end |