Class: Slingshot::Search::Search
- Inherits:
-
Object
- Object
- Slingshot::Search::Search
- Defined in:
- lib/slingshot/search.rb
Instance Attribute Summary collapse
-
#facets ⇒ Object
readonly
Returns the value of attribute facets.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#indices ⇒ Object
readonly
Returns the value of attribute indices.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#query(&block) ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #facet(name, options = {}, &block) ⇒ Object
- #fields(fields = []) ⇒ Object
- #filter(type, *options) ⇒ Object
- #from(value) ⇒ Object
- #highlight(*args) ⇒ Object
-
#initialize(*indices, &block) ⇒ Search
constructor
A new instance of Search.
- #logged(error = nil) ⇒ Object
- #perform ⇒ Object
- #size(value) ⇒ Object
- #sort(&block) ⇒ Object
- #to_curl ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(*indices, &block) ⇒ Search
Returns a new instance of Search.
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
#facets ⇒ Object (readonly)
Returns the value of attribute facets.
6 7 8 |
# File 'lib/slingshot/search.rb', line 6 def facets @facets end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
6 7 8 |
# File 'lib/slingshot/search.rb', line 6 def filters @filters end |
#indices ⇒ Object (readonly)
Returns the value of attribute indices.
6 7 8 |
# File 'lib/slingshot/search.rb', line 6 def indices @indices end |
#json ⇒ Object (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 |
#response ⇒ Object (readonly)
Returns the value of attribute response.
6 7 8 |
# File 'lib/slingshot/search.rb', line 6 def response @response end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
6 7 8 |
# File 'lib/slingshot/search.rb', line 6 def results @results end |
#url ⇒ Object (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, ={}, &block) @facets ||= {} @facets.update Facet.new(name, , &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, *) @filters ||= [] @filters << Filter.new(type, *).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. 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 |
#perform ⇒ Object
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_curl ⇒ Object
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_json ⇒ Object
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 |