Module: Workarea::Search::Query
- Extended by:
- ActiveSupport::Concern
- Includes:
- GlobalID::Identification
- Included in:
- AdminAssets, AdminCategories, AdminDiscounts, AdminFulfillmentSkus, AdminInventorySkus, AdminOrders, AdminPages, AdminPaymentTransactions, AdminPricingSkus, AdminProducts, AdminReleasables, AdminReleases, AdminSearch, AdminSegments, AdminUsers, Categorization, CategoryBrowse, HelpSearch, ProductSearch, RelatedHelp, RelatedProducts
- Defined in:
- app/queries/workarea/search/query.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #additional_options ⇒ Object
- #aggregations ⇒ Object
- #body ⇒ Object
- #id ⇒ Object
- #initialize(params = {}) ⇒ Object
- #load_model_from(document) ⇒ Object
- #loaded_results ⇒ Object
- #post_filter ⇒ Object
- #query ⇒ Object
- #query_string ⇒ Object
- #response ⇒ Object
- #results ⇒ Object
- #scroll(options = {}, &block) ⇒ Object
- #stats ⇒ Object
- #total ⇒ Object
Instance Method Details
#additional_options ⇒ Object
110 111 112 113 114 |
# File 'app/queries/workarea/search/query.rb', line 110 def Workarea.config..inject({}) do |, option| .merge(option => send(option)) end end |
#aggregations ⇒ Object
45 46 47 |
# File 'app/queries/workarea/search/query.rb', line 45 def aggregations {} end |
#body ⇒ Object
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() .delete_if { |_, v| v.blank? } end |
#id ⇒ Object
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_results ⇒ Object
86 87 88 |
# File 'app/queries/workarea/search/query.rb', line 86 def loaded_results @loaded_results ||= load_results(response) end |
#post_filter ⇒ Object
41 42 43 |
# File 'app/queries/workarea/search/query.rb', line 41 def post_filter {} end |
#query ⇒ Object
37 38 39 |
# File 'app/queries/workarea/search/query.rb', line 37 def query { match_all: {} } end |
#query_string ⇒ Object
33 34 35 |
# File 'app/queries/workarea/search/query.rb', line 33 def query_string @query_string ||= QueryString.new(params[:q]) end |
#response ⇒ Object
59 60 61 |
# File 'app/queries/workarea/search/query.rb', line 59 def response @response ||= self.class.document.search(body) end |
#results ⇒ Object
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( = {}, &block) = { scroll: Workarea.config.elasticsearch_default_scroll }.merge() results = self.class.document.search(body, ) scroll_id = results['_scroll_id'] yield load_results(results) while (results = self.class.document.scroll(scroll_id, )) && 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 |
#stats ⇒ Object
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 |
#total ⇒ Object
67 68 69 |
# File 'app/queries/workarea/search/query.rb', line 67 def total response['hits']['total'] end |