Sha256: 9528ff3301bb7eadb9a06333c350289dbd9d6987ca084144c9073feb1f4d212c

Contents?: true

Size: 1.01 KB

Versions: 31

Compression:

Stored size: 1.01 KB

Contents

module Workarea
  module Search
    class StorefrontSearch
      class ExactMatches
        include Middleware

        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

        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

31 entries across 31 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.44 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.43 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.42 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.41 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.40 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.39 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.38 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.37 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.36 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.35 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.34 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.33 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.32 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.31 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.30 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.29 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.28 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.27 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.26 app/queries/workarea/search/storefront_search/exact_matches.rb