class ElasticQueue::Query
Public Class Methods
new(queue, options = {})
click to toggle source
# File lib/elastic_queue/query.rb, line 6 def initialize(queue, options = {}) @queue = queue @options = QueryOptions.new(options) end
Public Instance Methods
all()
click to toggle source
# File lib/elastic_queue/query.rb, line 56 def all Results.new(@queue, execute, @options).instantiated_queue_items end
body()
click to toggle source
# File lib/elastic_queue/query.rb, line 38 def body @options.body end
count()
click to toggle source
# File lib/elastic_queue/query.rb, line 65 def count execute(count: true, paginate: false)[:hits][:total].to_i end
filter(options)
click to toggle source
# File lib/elastic_queue/query.rb, line 11 def filter(options) @options.add_filter(options) self end
filters()
click to toggle source
# File lib/elastic_queue/query.rb, line 16 def filters @options.filters end
ids()
click to toggle source
return just the ids of the records (useful when combined with SQL queries)
# File lib/elastic_queue/query.rb, line 61 def ids execute[:hits][:hits].map { |h| h[:_source][:id] } end
page=(page)
click to toggle source
TODO: remove if not using, add per_page if using
# File lib/elastic_queue/query.rb, line 52 def page=(page) @options.page = (page) end
paginate(options = {})
click to toggle source
# File lib/elastic_queue/query.rb, line 46 def paginate(options = {}) options.each { |k, v| @options.send("#{k}=", v) } Results.new(@queue, execute(paginate: true), @options).paginate end
percolator_body()
click to toggle source
# File lib/elastic_queue/query.rb, line 42 def percolator_body @options.percolator_body end
search(string)
click to toggle source
# File lib/elastic_queue/query.rb, line 29 def search(string) @options.add_search(string) self end
searches()
click to toggle source
# File lib/elastic_queue/query.rb, line 34 def searches @options.search end
sort(options)
click to toggle source
# File lib/elastic_queue/query.rb, line 20 def sort(options) @options.add_sort(options) self end
sorts()
click to toggle source
# File lib/elastic_queue/query.rb, line 25 def sorts @options.sorts end
Private Instance Methods
execute(count: false, paginate: false)
click to toggle source
# File lib/elastic_queue/query.rb, line 71 def execute(count: false, paginate: false) begin search = paginate ? execute_paginated_query : execute_all_query( count: count ) search = substitute_page(search) if paginate && !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
execute_all_query(count: false)
click to toggle source
# File lib/elastic_queue/query.rb, line 91 def execute_all_query(count: false) record_count = @queue.search_client.search index: @queue.index_name, body: body, search_type: 'count' return record_count if count @queue.search_client.search index: @queue.index_name, body: body, search_type: 'query_then_fetch', from: 0, size: record_count['hits']['total'].to_i end
execute_paginated_query()
click to toggle source
# File lib/elastic_queue/query.rb, line 97 def execute_paginated_query @queue.search_client.search index: @queue.index_name, body: body, search_type: 'query_then_fetch', from: @options.from, size: @options.per_page end
failed_search()
click to toggle source
# File lib/elastic_queue/query.rb, line 108 def failed_search { page: 0, hits: { hits: [], total: 0 } } end
method_missing(method, *args, &block)
click to toggle source
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
# File lib/elastic_queue/query.rb, line 84 def method_missing(method, *args, &block) if @queue.respond_to?(method) proc = @queue.scopes[method] instance_exec *args, &proc end end
substitute_page(search)
click to toggle source
# File lib/elastic_queue/query.rb, line 101 def substitute_page(search) total_hits = search['hits']['total'].to_i per_page = @options.per_page @options.page = (total_hits / per_page.to_f).ceil execute_paginated_query end