<commit_before>module Spree
  module Search
    class Sunspot < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain : Spree::Core::Search::Base
      def retrieve_products
        conf = Spree::Search.configuration

        # send(name) looks in @properties
        @properties[:sunspot] = ::Sunspot.search(Spree::Product) do
          # This is a little tricky to understand
          #     - we are sending the block value as a method
          #     - Spree::Search::Base is using method_missing() to return the param values
          conf.display_facets.each do |name|
            with("#{name}", send(name)) if send(name).present?
            facet("#{name}")
          end

          with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
          facet(:price) do
            conf.price_ranges.each do |range|
              row(range) do
                with(:price, Range.new(range.split('-').first, range.split('-').last))
              end
            end

            # TODO add greater than range
          end

          facet(:taxon_ids)
          with(:taxon_ids, send(:taxon).id) if send(:taxon)

          if send(:sort) == :score
            order_by :themesort
            order_by :position
            order_by :subposition
          end
          
          order_by sort.to_sym, order.to_sym
          with(:is_active, true)
          keywords(query)
          paginate(:page => page, :per_page => per_page)
        end

        self.sunspot.results
      end
      
      def retrieve_themes
        conf = Spree::Search.configuration

        # send(name) looks in @properties
        @properties[:sunspot] = ::Sunspot.search(Spree::Product) do
          # This is a little tricky to understand
          #     - we are sending the block value as a method
          #     - Spree::Search::Base is using method_missing() to return the param values
          conf.display_facets.each do |name|
            with("#{name}", send(name)) if send(name).present?
            facet("#{name}")
          end

          with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
          facet(:price) do
            conf.price_ranges.each do |range|
              row(range) do
                with(:price, Range.new(range.split('-').first, range.split('-').last))
              end
            end

            # TODO add greater than range
          end

          facet(:taxon_ids)
          with(:taxon_ids, send(:taxon).id) if send(:taxon)

          facet :themesort

          #facet :saletype
          #with(:saletype, send(:saletype)) if send(:saletype)

          order_by :themesort
          with(:is_active, true)
          with(:featured, true)
          
          keywords(query)
          paginate(:page => 1, :per_page => 180)
        end

        self.sunspot.results
      end

      protected

      def prepare(params)
        # super copies over :taxon and other variables into properties
        # as well as handles pagination
        super

        # TODO should do some parameter cleaning here: only allow valid search params to be passed through
        # the faceting partial is kind of 'dumb' about the params object: doesn't clean it out and just
        # dumps all the params into the query string

        @properties[:query] = params[:keywords]<commit_msg>Updated Searcher<commit_after>module Spree
  module Search
    class Sunspot < defined?(Spree::Search::MultiDomain) ? Spree::Search::MultiDomain : Spree::Core::Search::Base

      def retrieve_products
        retrieve_indexed.results
      end

      def retrieve_hits
        retrieve_indexed.hits
      end

      def retrieve_themes
        retrieve_themes.results
      end

      def retrieve_theme_hits
        retrieve_themes.hits
      end

      protected

      def retrieve_indexed
        conf = Spree::Search.configuration

        # send(name) looks in @properties
        @properties[:sunspot] = ::Sunspot.search(Spree::Product) do
          # This is a little tricky to understand
          #     - we are sending the block value as a method
          #     - Spree::Search::Base is using method_missing() to return the param values
          conf.display_facets.each do |name|
            with("#{name}", send(name)) if send(name).present?
            facet("#{name}")
          end

          with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
          facet(:price) do
            conf.price_ranges.each do |range|
              row(range) do
                with(:price, Range.new(range.split('-').first, range.split('-').last))
              end
            end

            # TODO add greater than range
          end

          facet(:taxon_ids)
          with(:taxon_ids, send(:taxon).id) if send(:taxon)

          if send(:sort) == :score
            order_by :themesort
            order_by :position
            order_by :subposition
          end

          order_by sort.to_sym, order.to_sym
          with(:is_active, true)
          keywords(query)
          paginate(:page => page, :per_page => per_page)
        end

        self.sunspot
      end

      def retrieve_themes
        conf = Spree::Search.configuration

        # send(name) looks in @properties
        @properties[:sunspot] = ::Sunspot.search(Spree::Product) do
          # This is a little tricky to understand
          #     - we are sending the block value as a method
          #     - Spree::Search::Base is using method_missing() to return the param values
          conf.display_facets.each do |name|
            with("#{name}", send(name)) if send(name).present?
            facet("#{name}")
          end

          with(:price, Range.new(price.split('-').first, price.split('-').last)) if price
          facet(:price) do
            conf.price_ranges.each do |range|
              row(range) do
                with(:price, Range.new(range.split('-').first, range.split('-').last))
              end
            end

            # TODO add greater than range
          end

          facet(:taxon_ids)
          with(:taxon_ids, send(:taxon).id) if send(:taxon)

          facet :themesort

          #facet :saletype
          #with(:saletype, send(:saletype)) if send(:saletype)
          #with(:featured, true)

          order_by :themesort
          with(:is_active, true)
          with(:featured, true)
          
          keywords(query)
          paginate(:page => 1, :per_page => 180)
        end

        self.sunspot
      end

      def prepare(params)
        # super copies over :taxon and other variables into properties
        # as well as handles pagination
        super

        # TODO should do some parameter cleaning here: only allow valid search params to be passed through
        # the faceting partial is kind of 'dumb' about the params object: doesn't clean it out and just
        # dumps all the params into the query string

        @properties[:query] = params[:keywords]
{'fraction_non_alphanumeric': 0.10273582217155885, 'fraction_numerical': 0.001139925904816187, 'mean_word_length': 1.8731068358575522, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4982174', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1876, 'n_words': 540}