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.
- #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
28 29 30 31 32 |
# File 'lib/slingshot/search.rb', line 28 def facet(name, ={}, &block) @facets ||= {} @facets.update Facet.new(name, , &block).to_hash self end |
#fields(fields = []) ⇒ Object
59 60 61 62 |
# File 'lib/slingshot/search.rb', line 59 def fields(fields=[]) @fields = fields self end |
#filter(type, *options) ⇒ Object
34 35 36 37 38 |
# File 'lib/slingshot/search.rb', line 34 def filter(type, *) @filters ||= [] @filters << Filter.new(type, *).to_hash self end |
#from(value) ⇒ Object
49 50 51 52 |
# File 'lib/slingshot/search.rb', line 49 def from(value) @from = value self end |
#highlight(*args) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/slingshot/search.rb', line 40 def highlight(*args) unless args.empty? @highlight = Highlight.new(*args) self else @highlight end end |
#perform ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/slingshot/search.rb', line 64 def perform @url = "#{Configuration.url}/#{indices.join(',')}/_search" @response = Configuration.client.post(@url, self.to_json) @json = Yajl::Parser.parse(@response) @results = Results::Collection.new(@json) self rescue Exception => e STDERR.puts "[REQUEST FAILED]\n#{self.to_curl}\n" raise ensure if Configuration.logger Configuration.logger.log_request '_search', indices, to_curl if Configuration.logger.level == 'debug' # FIXME: Depends on RestClient implementation if @response code = @response.code body = Yajl::Encoder.encode(@json, :pretty => true) else code = e. body = e.http_body end Configuration.logger.log_response code, body end end end |
#size(value) ⇒ Object
54 55 56 57 |
# File 'lib/slingshot/search.rb', line 54 def size(value) @size = value self end |
#sort(&block) ⇒ Object
23 24 25 26 |
# File 'lib/slingshot/search.rb', line 23 def sort(&block) @sort = Sort.new(&block) self end |
#to_curl ⇒ Object
90 91 92 |
# File 'lib/slingshot/search.rb', line 90 def to_curl %Q|curl -X POST "#{Configuration.url}/#{indices}/_search?pretty=true" -d '#{self.to_json}'| end |
#to_json ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/slingshot/search.rb', line 94 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 |