Class: Tire::Search::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indices = nil, options = {}, &block) ⇒ Search

Returns a new instance of Search.



9
10
11
12
13
14
15
16
17
# File 'lib/tire/search.rb', line 9

def initialize(indices=nil, options={}, &block)
  @indices = Array(indices)
  @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

Instance Attribute Details

#explain(value) ⇒ Object (readonly)

Returns the value of attribute explain.



7
8
9
# File 'lib/tire/search.rb', line 7

def explain
  @explain
end

#facetsObject (readonly)

Returns the value of attribute facets.



7
8
9
# File 'lib/tire/search.rb', line 7

def facets
  @facets
end

#filtersObject (readonly)

Returns the value of attribute filters.



7
8
9
# File 'lib/tire/search.rb', line 7

def filters
  @filters
end

#indicesObject (readonly)

Returns the value of attribute indices.



7
8
9
# File 'lib/tire/search.rb', line 7

def indices
  @indices
end

#jsonObject (readonly)

Returns the value of attribute json.



7
8
9
# File 'lib/tire/search.rb', line 7

def json
  @json
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/tire/search.rb', line 7

def options
  @options
end

#query(&block) ⇒ Object (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/tire/search.rb', line 7

def query
  @query
end

Instance Method Details

#facet(name, options = {}, &block) ⇒ Object



46
47
48
49
50
# File 'lib/tire/search.rb', line 46

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

#fields(*fields) ⇒ Object



79
80
81
82
# File 'lib/tire/search.rb', line 79

def fields(*fields)
  @fields = Array(fields.flatten)
  self
end

#filter(type, *options) ⇒ Object



52
53
54
55
56
# File 'lib/tire/search.rb', line 52

def filter(type, *options)
  @filters ||= []
  @filters << Filter.new(type, *options).to_hash
  self
end

#from(value) ⇒ Object



67
68
69
70
71
# File 'lib/tire/search.rb', line 67

def from(value)
  @from = value
  @options[:from] = value
  self
end

#highlight(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/tire/search.rb', line 58

def highlight(*args)
  unless args.empty?
    @highlight = Highlight.new(*args)
    self
  else
    @highlight
  end
end

#logged(error = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/tire/search.rb', line 134

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

#paramsObject



31
32
33
# File 'lib/tire/search.rb', line 31

def params
  @options.empty? ? '' : '?' + @options.to_param
end

#performObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tire/search.rb', line 93

def perform
  @response = Configuration.client.get(self.url + self.params, 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

#responseObject



23
24
25
# File 'lib/tire/search.rb', line 23

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

#resultsObject



19
20
21
# File 'lib/tire/search.rb', line 19

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

#size(value) ⇒ Object



73
74
75
76
77
# File 'lib/tire/search.rb', line 73

def size(value)
  @size = value
  @options[:size] = value
  self
end

#sort(&block) ⇒ Object



41
42
43
44
# File 'lib/tire/search.rb', line 41

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

#to_curlObject



106
107
108
# File 'lib/tire/search.rb', line 106

def to_curl
  %Q|curl -X GET "#{url}#{params.empty? ? '?' : params.to_s + '&'}pretty=true" -d '#{to_json}'|
end

#to_hashObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tire/search.rb', line 110

def to_hash
  @options.delete(:payload) || begin
    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.update( { :version => @version } )         if @version
    request.update( { :explain => @explain } )         if @explain
    request
  end
end

#to_jsonObject



128
129
130
131
132
# File 'lib/tire/search.rb', line 128

def to_json
  payload = to_hash
  # TODO: Remove when deprecated interface is removed
  payload.is_a?(String) ? payload : payload.to_json
end

#urlObject



27
28
29
# File 'lib/tire/search.rb', line 27

def url
  Configuration.url + @path
end

#version(value) ⇒ Object



89
90
91
# File 'lib/tire/search.rb', line 89

def version(value)
  @version = value
end