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.25 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.24 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.23 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.22 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.21 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.20 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.19 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.18 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.17 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.16 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.4.15 app/queries/workarea/search/storefront_search/exact_matches.rb