<commit_before>class Event < ActiveRecord::Base
  class Search
    Sunspot = Struct.new(:query, :opts) do
      # Return an Array of non-duplicate Event instances matching the search +query+..
      #
      # Options:
      # * :order => How to order the entries? Defaults to :score. Permitted values:
      #   * :score => Sort with most relevant matches first
      #   * :date => Sort by date
      #   * :name => Sort by event title
      #   * :title => same as :name
      #   * :venue => Sort by venue title
      # * :limit => Maximum number of entries to return. Defaults to +solr_search_matches+.
      # * :skip_old => Return old entries? Defaults to false.
      def self.search(*args)
        new(*args).search
      end

      def initialize(*args)
        super
        configure unless configured?
      end

      def search
        Event.solr_search do
          keywords query, minimum_match: 1
          order_by *order
          order_by :start_time, :desc
          with :duplicate, false
          with(:start_time).greater_than(Date.yesterday.to_time) if skip_old
          data_accessor_for(Event).include = [:venue]
        end.results.take(limit)
      end

      private

      def order
        case opts[:order].try(:to_sym)
        when :date
          [:start_time, :desc]
        when :venue, :location
          [:venue_title, :asc]
        when :name, :title
          [:title, :asc]
        else
          [:score, :desc]
        end
      end

      def skip_old
        opts[:skip_old] == true
      end

      def limit
        opts[:limit] || 50
      end

      def configure
        Event.searchable do
          text :title, :default_boost => 3
          string :title

          text :description

          text :tag_list, :default_boost => 3

          text :url

          time :start_time
          time :end_time

          text :venue_title
          string :venue_title

          boolean(:duplicate) { |event| event.duplicate? }
        end
      end

      def configured?
        Event.respond_to?(:solr_search)<commit_msg>prep Sunspot during initial Event::Search::Sunspot configuration.<commit_after>class Event < ActiveRecord::Base
  class Search
    Sunspot = Struct.new(:query, :opts) do
      # Return an Array of non-duplicate Event instances matching the search +query+..
      #
      # Options:
      # * :order => How to order the entries? Defaults to :score. Permitted values:
      #   * :score => Sort with most relevant matches first
      #   * :date => Sort by date
      #   * :name => Sort by event title
      #   * :title => same as :name
      #   * :venue => Sort by venue title
      # * :limit => Maximum number of entries to return. Defaults to +solr_search_matches+.
      # * :skip_old => Return old entries? Defaults to false.
      def self.search(*args)
        new(*args).search
      end

      def initialize(*args)
        super
        configure unless configured?
      end

      def search
        Event.solr_search do
          keywords query, minimum_match: 1
          order_by *order
          order_by :start_time, :desc
          with :duplicate, false
          with(:start_time).greater_than(Date.yesterday.to_time) if skip_old
          data_accessor_for(Event).include = [:venue]
        end.results.take(limit)
      end

      private

      def order
        case opts[:order].try(:to_sym)
        when :date
          [:start_time, :desc]
        when :venue, :location
          [:venue_title, :asc]
        when :name, :title
          [:title, :asc]
        else
          [:score, :desc]
        end
      end

      def skip_old
        opts[:skip_old] == true
      end

      def limit
        opts[:limit] || 50
      end

      def configure
        Event.searchable do
          text :title, :default_boost => 3
          string :title

          text :description

          text :tag_list, :default_boost => 3

          text :url

          time :start_time
          time :end_time

          text :venue_title
          string :venue_title

          boolean(:duplicate) { |event| event.duplicate? }
        end
        Event.reindex
        ::Sunspot.commit
      end

      def configured?
        Event.respond_to?(:solr_search)
{'fraction_non_alphanumeric': 0.09146484834234658, 'fraction_numerical': 0.0023512814483893723, 'mean_word_length': 1.7785760940561723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '3848440', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1173, 'n_words': 358}