Class: ElasticQueue::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_queue/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, options = {}) ⇒ Query

Returns a new instance of Query.



6
7
8
9
# File 'lib/elastic_queue/query.rb', line 6

def initialize(queue, options = {})
  @queue = queue
  @options = QueryOptions.new(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

this allows you to chain scopes the 2+ scopes in the chain will be called on a query object and not on the base object



85
86
87
88
89
90
# File 'lib/elastic_queue/query.rb', line 85

def method_missing(method, *args, &block)
  if @queue.respond_to?(method)
    proc = @queue.scopes[method]
    instance_exec *args, &proc
  end
end

Instance Method Details

#allObject



55
56
57
# File 'lib/elastic_queue/query.rb', line 55

def all
  @results ||= Results.new(@queue, execute, @options)
end

#bodyObject



38
39
40
# File 'lib/elastic_queue/query.rb', line 38

def body
  @options.body
end

#countObject



65
66
67
68
# File 'lib/elastic_queue/query.rb', line 65

def count
  res = execute(count: true)
  res[:hits][:total].to_i
end

#execute(count: false) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/elastic_queue/query.rb', line 70

def execute(count: false)
  begin
    search = execute_query(count: false)
    search = substitute_page(search) if !count && search['hits']['hits'].length == 0 && search['hits']['total'] != 0
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
    search = failed_search
  end
  search.with_indifferent_access
end

#filter(options) ⇒ Object



11
12
13
14
# File 'lib/elastic_queue/query.rb', line 11

def filter(options)
  @options.add_filter(options)
  self
end

#filtersObject



16
17
18
# File 'lib/elastic_queue/query.rb', line 16

def filters
  @options.filters
end

#idsObject

return just the ids of the records (useful when combined with SQL queries)



60
61
62
63
# File 'lib/elastic_queue/query.rb', line 60

def ids
  results = execute
  results[:hits][:hits].map { |h| h[:_source][:id] }
end

#page=(page) ⇒ Object



51
52
53
# File 'lib/elastic_queue/query.rb', line 51

def page=(page)
  @options.page = (page)
end

#paginate(options = {}) ⇒ Object



46
47
48
49
# File 'lib/elastic_queue/query.rb', line 46

def paginate(options = {})
  options.each { |k, v| @options.send("#{k}=", v) }
  all.paginate
end

#percolator_bodyObject



42
43
44
# File 'lib/elastic_queue/query.rb', line 42

def percolator_body
  @options.percolator_body
end

#search(string) ⇒ Object



29
30
31
32
# File 'lib/elastic_queue/query.rb', line 29

def search(string)
  @options.add_search(string)
  self
end

#searchesObject



34
35
36
# File 'lib/elastic_queue/query.rb', line 34

def searches
  @options.search
end

#sort(options) ⇒ Object



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

def sort(options)
  @options.add_sort(options)
  self
end

#sortsObject



25
26
27
# File 'lib/elastic_queue/query.rb', line 25

def sorts
  @options.sorts
end