<commit_before>module Tire
  module Search
    class SearchRequestFailed < StandardError; end

    class Search

      attr_reader :indices, :types, :query, :facets, :filters, :options, :explain, :script_fields

      def initialize(indices=nil, options={}, &block)
        if indices.is_a?(Hash)
          set_indices_options(indices)
          @indices = indices.keys
        else
          @indices = Array(indices)
        end
        @types   = Array(options.delete(:type)).map { |type| Utils.escape(type) }
        @options = options

        @path    = ['/', @indices.join(','), @types.join(','), '_search'].compact.join('/').squeeze('/')

        block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
      end


      def set_indices_options(indices)
        indices.each do |index, index_options|
          if index_options[:boost]
            @indices_boost ||= {}
            @indices_boost[index] = index_options[:boost]
          end
        end
      end

      def results
        @results  || (perform; @results)
      end

      def response
        @response || (perform; @response)
      end

      def json
        @json     || (perform; @json)
      end

      def url
        Configuration.url + @path
      end

      def params
        options = @options.except(:wrapper, :payload, :load)
        options.empty? ? '' : '?' + options.to_param
      end

      def query(&block)
        @query = Query.new
        block.arity < 1 ? @query.instance_eval(&block) : block.call(@query)
        self
      end

      def sort(&block)
        @sort = Sort.new(&block).to_ary
        self
      end

      def facet(name, options={}, &block)
        @facets ||= {}
        @facets.update Facet.new(name, options, &block).to_hash
        self
      end

      def filter(type, *options)
        @filters ||= []
        @filters << Filter.new(type, *options).to_hash
        self
      end<commit_msg>support per-search url settings<commit_after>module Tire
  module Search
    class SearchRequestFailed < StandardError; end

    class Search

      attr_reader :indices, :types, :query, :facets, :filters, :options, :explain, :script_fields

      def initialize(indices=nil, options={}, &block)
        if indices.is_a?(Hash)
          set_indices_options(indices)
          @indices = indices.keys
        else
          @indices = Array(indices)
        end
        @types   = Array(options.delete(:type)).map { |type| Utils.escape(type) }
        @config_url = options.delete(:config_url) # allow per-search url changes
        @options = options

        @path    = ['/', @indices.join(','), @types.join(','), '_search'].compact.join('/').squeeze('/')

        block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
      end


      def set_indices_options(indices)
        indices.each do |index, index_options|
          if index_options[:boost]
            @indices_boost ||= {}
            @indices_boost[index] = index_options[:boost]
          end
        end
      end

      def results
        @results  || (perform; @results)
      end

      def response
        @response || (perform; @response)
      end

      def json
        @json     || (perform; @json)
      end

      def url
        (@config_url || Configuration.url) + @path
      end

      def params
        options = @options.except(:wrapper, :payload, :load)
        options.empty? ? '' : '?' + options.to_param
      end

      def query(&block)
        @query = Query.new
        block.arity < 1 ? @query.instance_eval(&block) : block.call(@query)
        self
      end

      def sort(&block)
        @sort = Sort.new(&block).to_ary
        self
      end

      def facet(name, options={}, &block)
        @facets ||= {}
        @facets.update Facet.new(name, options, &block).to_hash
        self
      end

      def filter(type, *options)
        @filters ||= []
        @filters << Filter.new(type, *options).to_hash
        self
      end
{'fraction_non_alphanumeric': 0.12788365095285859, 'fraction_numerical': 0.0010030090270812437, 'mean_word_length': 2.00150489089541, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '2312376', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1153, 'n_words': 271}