class Tire::Search::Search

Attributes

facets[R]
filters[R]
indices[R]
json[R]
options[R]
query[R]

Public Class Methods

new(indices=nil, options = {}, &block) click to toggle source
# File lib/tire/search.rb, line 9
def initialize(indices=nil, options = {}, &block)
  @indices = Array(indices)
  @types   = Array(options.delete(: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

Public Instance Methods

facet(name, options={}, &block) click to toggle source
# File lib/tire/search.rb, line 42
def facet(name, options={}, &block)
  @facets ||= {}
  @facets.update Facet.new(name, options, &block).to_hash
  self
end
fields(*fields) click to toggle source
# File lib/tire/search.rb, line 75
def fields(*fields)
  @fields = Array(fields.flatten)
  self
end
filter(type, *options) click to toggle source
# File lib/tire/search.rb, line 48
def filter(type, *options)
  @filters ||= []
  @filters << Filter.new(type, *options).to_hash
  self
end
from(value) click to toggle source
# File lib/tire/search.rb, line 63
def from(value)
  @from = value
  @options[:from] = value
  self
end
highlight(*args) click to toggle source
# File lib/tire/search.rb, line 54
def highlight(*args)
  unless args.empty?
    @highlight = Highlight.new(*args)
    self
  else
    @highlight
  end
end
logged(error=nil) click to toggle source
# File lib/tire/search.rb, line 115
def logged(error=nil)
  if Configuration.logger

    Configuration.logger.log_request '_search', indices, to_curl

    took = @json['took']  rescue nil
    code = @response.code rescue nil

    if Configuration.logger.level.to_s == 'debug'
      # FIXME: Depends on RestClient implementation
      body = if @json
        defined?(Yajl) ? Yajl::Encoder.encode(@json, :pretty => true) : MultiJson.encode(@json)
      else
        @response.body rescue nil
      end
    else
      body = ''
    end

    Configuration.logger.log_response code || 'N/A', took || 'N/A', body || 'N/A'
  end
end
perform() click to toggle source
# File lib/tire/search.rb, line 80
def perform
  @response = Configuration.client.get(self.url, self.to_json)
  if @response.failure?
    STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
    raise SearchRequestFailed, @response.to_s
  end
  @json     = MultiJson.decode(@response.body)
  @results  = Results::Collection.new(@json, @options)
  return self
ensure
  logged
end
response() click to toggle source
# File lib/tire/search.rb, line 23
def response
  @response || (perform; @response)
end
results() click to toggle source
# File lib/tire/search.rb, line 19
def results
  @results  || (perform; @results)
end
size(value) click to toggle source
# File lib/tire/search.rb, line 69
def size(value)
  @size = value
  @options[:size] = value
  self
end
sort(&block) click to toggle source
# File lib/tire/search.rb, line 37
def sort(&block)
  @sort = Sort.new(&block).to_ary
  self
end
to_curl() click to toggle source
# File lib/tire/search.rb, line 93
def to_curl
  %Qcurl -X GET "#{self.url}?pretty=true" -d '#{self.to_json}'|
end
to_hash() click to toggle source
# File lib/tire/search.rb, line 97
def to_hash
  request = {}
  request.update( { :query  => @query.to_hash } )    if @query
  request.update( { :sort   => @sort.to_ary   } )    if @sort
  request.update( { :facets => @facets.to_hash } )   if @facets
  request.update( { :filter => @filters.first.to_hash } ) if @filters && @filters.size == 1
  request.update( { :filter => { :and => @filters.map { |filter| filter.to_hash } } } ) if  @filters && @filters.size > 1
  request.update( { :highlight => @highlight.to_hash } ) if @highlight
  request.update( { :size => @size } )               if @size
  request.update( { :from => @from } )               if @from
  request.update( { :fields => @fields } )           if @fields
  request
end
to_json() click to toggle source
# File lib/tire/search.rb, line 111
def to_json
  to_hash.to_json
end
url() click to toggle source
# File lib/tire/search.rb, line 27
def url
  Configuration.url + @path
end