Class: ElasticQueue::QueryOptions

Inherits:
Object
  • Object
show all
Includes:
Filters, Sorts
Defined in:
lib/elastic_queue/query_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sorts

#options_to_sorts

Methods included from Filters

#options_to_filters

Constructor Details

#initialize(options = {}) ⇒ QueryOptions

Returns a new instance of QueryOptions.


12
13
14
15
16
17
18
# File 'lib/elastic_queue/query_options.rb', line 12

def initialize(options = {})
  @options = { per_page: 30, page: 1 }.merge(options)
  @filters = { and: [] }.with_indifferent_access
  @sorts = []
  self.per_page = @options[:per_page]
  self.page = @options[:page]
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.


9
10
11
# File 'lib/elastic_queue/query_options.rb', line 9

def filters
  @filters
end

#pageObject

Returns the value of attribute page.


9
10
11
# File 'lib/elastic_queue/query_options.rb', line 9

def page
  @page
end

#per_pageObject

Returns the value of attribute per_page.


10
11
12
# File 'lib/elastic_queue/query_options.rb', line 10

def per_page
  @per_page
end

#searchObject (readonly)

Returns the value of attribute search.


9
10
11
# File 'lib/elastic_queue/query_options.rb', line 9

def search
  @search
end

#sortsObject (readonly)

Returns the value of attribute sorts.


9
10
11
# File 'lib/elastic_queue/query_options.rb', line 9

def sorts
  @sorts
end

Instance Method Details

#add_filter(options) ⇒ Object


20
21
22
# File 'lib/elastic_queue/query_options.rb', line 20

def add_filter(options)
  @filters[:and] += options_to_filters(options)
end

#add_search(string) ⇒ Object


28
29
30
# File 'lib/elastic_queue/query_options.rb', line 28

def add_search(string)
  @search = string
end

#add_sort(options) ⇒ Object


24
25
26
# File 'lib/elastic_queue/query_options.rb', line 24

def add_sort(options)
  @sorts += options_to_sorts(options)
end

#bodyObject


40
41
42
43
44
45
46
# File 'lib/elastic_queue/query_options.rb', line 40

def body
  b = {}
  b[:filter] = @filters unless @filters[:and].blank?
  b[:sort] = @sorts unless @sorts.blank?
  b[:query] = { query_string: { query: @search } } unless @search.blank?
  b
end

#fromObject


32
33
34
# File 'lib/elastic_queue/query_options.rb', line 32

def from
  (page - 1) * per_page
end

#percolator_bodyObject


48
49
50
51
52
# File 'lib/elastic_queue/query_options.rb', line 48

def percolator_body
  b = {}
  b[:filter] = @filters unless @filters[:and].blank?
  { query: { constant_score: b } }
end