Class: Slingshot::Search::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*indices, &block) ⇒ Search

Returns a new instance of Search.

Raises:

  • (ArgumentError)


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

def initialize(*indices, &block)
  @options = indices.pop if indices.last.is_a?(Hash)
  @indices = indices
  raise ArgumentError, 'Please pass index or indices to search' if @indices.empty?
  if @options
    Configuration.wrapper @options[:wrapper] if @options[:wrapper]
  end
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def facets
  @facets
end

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def filters
  @filters
end

#indicesObject (readonly)

Returns the value of attribute indices.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def indices
  @indices
end

#jsonObject (readonly)

Returns the value of attribute json.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def json
  @json
end

#query(&block) ⇒ Object (readonly)

Returns the value of attribute query.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def response
  @response
end

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def results
  @results
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/slingshot/search.rb', line 6

def url
  @url
end

Instance Method Details

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



29
30
31
32
33
# File 'lib/slingshot/search.rb', line 29

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

#fields(fields = []) ⇒ Object



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

def fields(fields=[])
  @fields = fields
  self
end

#filter(type, *options) ⇒ Object



35
36
37
38
39
# File 'lib/slingshot/search.rb', line 35

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

#from(value) ⇒ Object



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

def from(value)
  @from = value
  self
end

#highlight(*args) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/slingshot/search.rb', line 41

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

#logged(error = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/slingshot/search.rb', line 95

def logged(error=nil)
  if Configuration.logger

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

    code = @response ? @response.code : error.message
    took = @json['took'] rescue nil

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

    Configuration.logger.log_response code, took, body
  end
end

#performObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/slingshot/search.rb', line 65

def perform
  @url      = "#{Configuration.url}/#{indices.join(',')}/_search"
  @response = Configuration.client.post(@url, self.to_json)
  @json     = Yajl::Parser.parse(@response.body)
  @results  = Results::Collection.new(@json, @options)
  self
rescue Exception => error
  STDERR.puts "[REQUEST FAILED] #{self.to_curl}\n"
  raise
ensure
  logged(error)
end

#size(value) ⇒ Object



55
56
57
58
# File 'lib/slingshot/search.rb', line 55

def size(value)
  @size = value
  self
end

#sort(&block) ⇒ Object



24
25
26
27
# File 'lib/slingshot/search.rb', line 24

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

#to_curlObject



78
79
80
# File 'lib/slingshot/search.rb', line 78

def to_curl
  %Q|curl -X POST "#{Configuration.url}/#{indices.join(',')}/_search?pretty=true" -d '#{self.to_json}'|
end

#to_jsonObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/slingshot/search.rb', line 82

def to_json
  request = {}
  request.update( { :query  => @query } )
  request.update( { :sort   => @sort } )     if @sort
  request.update( { :facets => @facets } )   if @facets
  @filters.each { |filter| request.update( { :filter => filter } ) } if @filters
  request.update( { :highlight => @highlight } ) if @highlight
  request.update( { :size => @size } )       if @size
  request.update( { :from => @from } )       if @from
  request.update( { :fields => @fields } )   if @fields
  Yajl::Encoder.encode(request)
end