Module: Goodyear::QueryMethods

Includes:
BooleanMethods, FacetMethods, FilterMethods, FinderMethods, QueryCache
Defined in:
lib/goodyear/query_methods.rb

Instance Method Summary collapse

Methods included from QueryCache

#cache_query, #store

Methods included from FilterMethods

#filter, #has_field?, #query_filter

Methods included from FacetMethods

#facet

Methods included from BooleanMethods

#or

Methods included from FinderMethods

#all, #fields, #first, #highlight, #last, #size, #sort, #where

Instance Method Details

#countObject



71
72
73
74
# File 'lib/goodyear/query_methods.rb', line 71

def count
  search_type 'count'
  fetch.total
end

#fetchObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/goodyear/query_methods.rb', line 17

def fetch
  es = self.perform
  options = {wrapper: self, type: document_type}
  options.merge!( @_search_options ) unless @_search_options.nil?

  @_search_options = nil

  tire = Tire::Search::Search.new(self.index_name, options)
  if es.query_filters.empty?
    tire.query { string es.query } unless es.query.blank?
  else
    tire.query do
      filtered do
        query { string es.query } unless es.query.blank?
        es.query_filters.each { |f| filter(f[:name], f[:options]) }
      end
    end
  end

  tire.sort{ by *es.sort } unless es.sort.blank?
  tire.size( es.size ) unless es.size.nil?
  tire.fields( es.fields ) unless es.fields.empty?
  tire.highlight( es.highlights ) unless es.highlights.empty?

  es.filters.each do |f|
    tire.filter(f[:name], f[:args], &f[:l])
  end

  es.facets.each do |f|
    tire.facet(f[:name], f[:args], &f[:l] )
  end

  cache_query(tire.to_curl) { tire.version(true).results }

end

#performObject Also known as: to_query



53
54
55
56
57
58
# File 'lib/goodyear/query_methods.rb', line 53

def perform
  construct_query
  esq = Query.new(@_query, @_fields, @_size, @_sort, @_highlights, @_facets, @_filters, @_query_filters)
  clean
  return esq
end

#reset_queryObject



98
99
100
# File 'lib/goodyear/query_methods.rb', line 98

def reset_query
  clean
end

#resultsObject



76
77
78
# File 'lib/goodyear/query_methods.rb', line 76

def results
  fetch.results
end

#routing(r) ⇒ Object



80
81
82
83
84
# File 'lib/goodyear/query_methods.rb', line 80

def routing(r)
  @_search_options ||= {}
  @_search_options.merge! routing: r
  self
end

#scope(name, scope_options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/goodyear/query_methods.rb', line 86

def scope(name, scope_options = {})
  name = name.to_sym

  scope_proc = lambda do |*args|
    options = scope_options.respond_to?(:call) ? scope_options.call(*args) : scope_options
  end

  singleton_class.send(:define_method, name, &scope_proc)
end

#search_options(options) ⇒ Object



60
61
62
63
# File 'lib/goodyear/query_methods.rb', line 60

def search_options(options)
  @_search_options = options
  self
end

#search_type(type) ⇒ Object



65
66
67
68
69
# File 'lib/goodyear/query_methods.rb', line 65

def search_type(type)
  @_search_options ||= {}
  @_search_options.merge! search_type: type
  self
end