Class: Workarea::Search::StorefrontSearch::ExactMatches

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
app/queries/workarea/search/storefront_search/exact_matches.rb

Instance Method Summary collapse

Methods included from Middleware

#initialize

Instance Method Details

#call(response) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/queries/workarea/search/storefront_search/exact_matches.rb', line 7

def call(response)
  exact_match = find_exact_match(response)

  if response.customization.new_record? && !response.has_filters? && exact_match.present?
    response.redirect = product_path(exact_match)
  else
    yield
  end
end

#find_exact_match(response) ⇒ Object



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

def find_exact_match(response)
  exact_matches = find_exact_matches(response)

  # Depending on boost settings and configs, sometimes scores can exceed
  # the exact match threshold. Only render an exact match if it's a single match.
  return unless exact_matches.one?

  Elasticsearch::Serializer.deserialize(exact_matches.first['_source'])
end

#find_exact_matches(response) ⇒ Object



27
28
29
30
# File 'app/queries/workarea/search/storefront_search/exact_matches.rb', line 27

def find_exact_matches(response)
  hits = response.query.response.dig('hits', 'hits')
  hits.select { |h| h['_score'] >= Workarea.config.search_exact_match_score }
end