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
18
19
20
21
22
# File 'lib/tire/search.rb', line 9

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

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

#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

#script_fieldsObject (readonly)

Returns the value of attribute script_fields.



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

def script_fields
  @script_fields
end

Instance Method Details

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



65
66
67
68
69
# File 'lib/tire/search.rb', line 65

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

#fields(*fields) ⇒ Object



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

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

#filter(type, *options) ⇒ Object



71
72
73
74
75
# File 'lib/tire/search.rb', line 71

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

#from(value) ⇒ Object



92
93
94
95
96
# File 'lib/tire/search.rb', line 92

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

#highlight(*args) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/tire/search.rb', line 83

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

#jsonObject



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

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

#logged(error = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tire/search.rb', line 162

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 LoadBalanceClient 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



50
51
52
# File 'lib/tire/search.rb', line 50

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

#performObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tire/search.rb', line 119

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



38
39
40
# File 'lib/tire/search.rb', line 38

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

#resultsObject



34
35
36
# File 'lib/tire/search.rb', line 34

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

#script_field(name, options = {}) ⇒ Object



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

def script_field(name, options={})
  @script_fields ||= {}
  @script_fields.merge! ScriptField.new(name, options).to_hash
  self
end

#set_indices_options(indices) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/tire/search.rb', line 25

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

#size(value) ⇒ Object



98
99
100
101
102
# File 'lib/tire/search.rb', line 98

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

#sort(&block) ⇒ Object



60
61
62
63
# File 'lib/tire/search.rb', line 60

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

#to_curlObject



132
133
134
# File 'lib/tire/search.rb', line 132

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

#to_hashObject



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

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

#to_jsonObject



156
157
158
159
160
# File 'lib/tire/search.rb', line 156

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

#urlObject



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

def url
  Configuration.url + @path
end

#version(value) ⇒ Object



114
115
116
117
# File 'lib/tire/search.rb', line 114

def version(value)
  @version = value
  self
end