Module: Workarea::Search::Query

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#additional_optionsObject



110
111
112
113
114
# File 'app/queries/workarea/search/query.rb', line 110

def additional_options
  Workarea.config.search_query_options.inject({}) do |options, option|
    options.merge(option => send(option))
  end
end

#aggregationsObject



45
46
47
# File 'app/queries/workarea/search/query.rb', line 45

def aggregations
  {}
end

#bodyObject



49
50
51
52
53
54
55
56
57
# File 'app/queries/workarea/search/query.rb', line 49

def body
  {
    query: query,
    post_filter: post_filter,
    aggs: aggregations
  }
  .merge(additional_options)
  .delete_if { |_, v| v.blank? }
end

#idObject



29
30
31
# File 'app/queries/workarea/search/query.rb', line 29

def id
  params.to_json
end

#initialize(params = {}) ⇒ Object



25
26
27
# File 'app/queries/workarea/search/query.rb', line 25

def initialize(params = {})
  @params = params.to_h.with_indifferent_access
end

#load_model_from(document) ⇒ Object



106
107
108
# File 'app/queries/workarea/search/query.rb', line 106

def load_model_from(document)
  Elasticsearch::Serializer.deserialize(document['_source'])
end

#loaded_resultsObject



86
87
88
# File 'app/queries/workarea/search/query.rb', line 86

def loaded_results
  @loaded_results ||= load_results(response)
end

#post_filterObject



41
42
43
# File 'app/queries/workarea/search/query.rb', line 41

def post_filter
  {}
end

#queryObject



37
38
39
# File 'app/queries/workarea/search/query.rb', line 37

def query
  { match_all: {} }
end

#query_stringObject



33
34
35
# File 'app/queries/workarea/search/query.rb', line 33

def query_string
  @query_string ||= QueryString.new(params[:q])
end

#responseObject



59
60
61
# File 'app/queries/workarea/search/query.rb', line 59

def response
  @response ||= self.class.document.search(body)
end

#resultsObject



63
64
65
# File 'app/queries/workarea/search/query.rb', line 63

def results
  @results ||= loaded_results
end

#scroll(options = {}, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/queries/workarea/search/query.rb', line 71

def scroll(options = {}, &block)
  options = { scroll: Workarea.config.elasticsearch_default_scroll }.merge(options)
  results = self.class.document.search(body, options)
  scroll_id = results['_scroll_id']

  yield load_results(results)

  while (results = self.class.document.scroll(scroll_id, options)) && results['hits']['hits'].present?
    yield load_results(results)
    scroll_id = results['_scroll_id']
  end
ensure
  self.class.document.clear_scroll(scroll_id) if scroll_id.present?
end

#statsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/queries/workarea/search/query.rb', line 90

def stats
  return @stats if defined?(@stats)

  @stats = {}
  return @stats if response['aggregations'].blank?

  response['aggregations'].each do |name, data|
    if data['count'].present? || data[name].present?
      @stats[name] = data[name].present? ? data[name] : data
    end
  end

  @stats.each { |k, v| @stats.delete(k) if v.empty? }
  @stats
end

#totalObject



67
68
69
# File 'app/queries/workarea/search/query.rb', line 67

def total
  response['hits']['total']
end