Sha256: d90d816b98cccdc25328e4273d15b2630100b10230231ae8e290b84122689687

Contents?: true

Size: 1000 Bytes

Versions: 3

Compression:

Stored size: 1000 Bytes

Contents

module Workarea
  module Search
    class StorefrontSearch
      class ExactMatches
        include Middleware

        def call(response)
          exact_match = find_exact_match(response)

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

        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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
workarea-core-3.4.14 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.13 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.12 app/queries/workarea/search/storefront_search/exact_matches.rb