Sha256: 9e5f632e324966ceda711bd940c26c446fdbc93c8191e34329c67b06a36f7cf0

Contents?: true

Size: 1.03 KB

Versions: 28

Compression:

Stored size: 1.03 KB

Contents

module Workarea
  module Search
    class StorefrontSearch
      class ExactMatches
        include StorefrontSearch::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

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.6 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.5 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.4 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.3 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.2 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.1 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.0 app/queries/workarea/search/storefront_search/exact_matches.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/search/storefront_search/exact_matches.rb