Class: Workarea::Search::ProductSearch

Inherits:
Object
  • Object
show all
Includes:
Facets, LoadProductResults, Pagination, ProductDisplayRules, ProductRulesFiltering, Query, QuerySuggestions
Defined in:
app/queries/workarea/search/product_search.rb

Constant Summary collapse

PASSES =
[0, 1, 2]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoadProductResults

#load_inventory_from, #load_model_from, #load_pricing_from

Methods included from Pagination

#each_by, #from, #page, #per_page, #results, #size

Methods included from QuerySuggestions

#query_suggestion_response, #query_suggestions, #sanitized_query, #should_use_suggestion?, #suggest

Methods included from ProductRulesFiltering

#product_rule_filters, #product_rules_query_clauses

Methods included from ProductDisplayRules

#active_for_segments_clause, #displayable_when_out_of_stock_sort_clause, #inventory_display_clause, #preview_current_release_clause, #product_display_query_clauses

Methods included from Facets

#facets, #range_facets, #terms_facets

Methods included from Query

#additional_options, #body, #id, #initialize, #load_model_from, #loaded_results, #query_string, #response, #results, #scroll, #stats, #total

Class Method Details

.available_sortsObject



16
17
18
19
20
21
22
23
24
25
# File 'app/queries/workarea/search/product_search.rb', line 16

def self.available_sorts
  Sort::Collection.new(
    Sort.relevance,
    Sort.top_sellers,
    Sort.popularity,
    Sort.newest,
    Sort.price_asc,
    Sort.price_desc
  )
end

Instance Method Details

#aggregationsObject



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

def aggregations
  facets.map(&:aggregation).reduce(&:merge)
end

#boosted_fieldsObject



71
72
73
74
75
76
# File 'app/queries/workarea/search/product_search.rb', line 71

def boosted_fields
  text_fields = pass > PASSES.first ? boosts : boosts.except(:description)
  text_fields.map do |name, boost|
    "content.#{name}^#{boost || 1}"
  end
end

#boostsObject



64
65
66
67
68
69
# File 'app/queries/workarea/search/product_search.rb', line 64

def boosts
  result = Search::Settings.current.boosts.presence ||
            Workarea.config.default_search_boosts

  result.with_indifferent_access
end

#customizationObject



57
58
59
60
61
62
# File 'app/queries/workarea/search/product_search.rb', line 57

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

  result = Customization.find_by_query(params[:q])
  @customization = result.active? ? result : Customization.new
end

#default_operatorObject



78
79
80
# File 'app/queries/workarea/search/product_search.rb', line 78

def default_operator
  pass > PASSES.second ? 'OR' : 'AND'
end

#passObject



53
54
55
# File 'app/queries/workarea/search/product_search.rb', line 53

def pass
  params[:pass] || PASSES.first
end

#post_filterObject



45
46
47
48
49
50
51
# File 'app/queries/workarea/search/product_search.rb', line 45

def post_filter
  {
    bool: {
      must: facets.map(&:post_filter_clause).reject(&:blank?)
    }
  }
end

#queryObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/queries/workarea/search/product_search.rb', line 27

def query
  result = {
    bool: {
      must: product_display_query_clauses + product_rules_query_clauses
    }
  }

  if params[:q].present? && !query_string.all?
    result[:bool][:must] << query_string_clause
  end

  result
end

#selected_sortObject



120
121
122
# File 'app/queries/workarea/search/product_search.rb', line 120

def selected_sort
  self.class.available_sorts.find(params[:sort])
end

#sortObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/queries/workarea/search/product_search.rb', line 82

def sort
  result = []

  if selected_sort.field.present?
    result << {
      selected_sort.field => {
        order: selected_sort.direction,
        missing: '_last',
        unmapped_type: 'float'
      }
    }
  else
    result << {
      "sorts.#{query_string.id}" => {
        order: 'asc',
        missing: '_last',
        unmapped_type: 'float'
      }
    }
  end

  result << displayable_when_out_of_stock_sort_clause
  result << { _score: :desc }
  result << {
    'sorts.orders_score': {
      order: 'desc',
      missing: '_last',
      unmapped_type: 'float'
    }
  }

  result
end

#sortsObject



116
117
118
# File 'app/queries/workarea/search/product_search.rb', line 116

def sorts
  Array(params[:sort])
end