Class: Searchkick::Query
- Inherits:
-
Object
- Object
- Searchkick::Query
- Extended by:
- Forwardable
- Defined in:
- lib/searchkick/query.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
Instance Method Summary collapse
- #execute ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(klass, term, options = {}) ⇒ Query
constructor
A new instance of Query.
- #params ⇒ Object
- #searchkick_index ⇒ Object
- #searchkick_klass ⇒ Object
- #searchkick_options ⇒ Object
- #to_curl ⇒ Object
Constructor Details
#initialize(klass, term, options = {}) ⇒ Query
Returns a new instance of Query.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/searchkick/query.rb', line 15 def initialize(klass, term, = {}) if term.is_a?(Hash) = term term = "*" else term = term.to_s end if [:emoji] term = EmojiParser.parse_unicode(term) { |e| " #{e.name} " }.strip end @klass = klass @term = term @options = @match_suffix = [:match] || [:match] || "analyzed" # prevent Ruby warnings @type = nil @routing = nil @misspellings_below = nil @highlighted_fields = nil prepare end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/searchkick/query.rb', line 6 def body @body end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def @options end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
5 6 7 |
# File 'lib/searchkick/query.rb', line 5 def term @term end |
Instance Method Details
#execute ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/searchkick/query.rb', line 73 def execute @execute ||= begin begin response = execute_search if @misspellings_below && response["hits"]["total"] < @misspellings_below prepare response = execute_search end rescue => e # TODO rescue type handle_error(e) end handle_response(response) end end |
#handle_response(response) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/searchkick/query.rb', line 99 def handle_response(response) # apply facet limit in client due to # https://github.com/elasticsearch/elasticsearch/issues/1305 @facet_limits.each do |field, limit| field = field.to_s facet = response["facets"][field] response["facets"][field]["terms"] = facet["terms"].first(limit) response["facets"][field]["other"] = facet["total"] - facet["terms"].sum { |term| term["count"] } end opts = { page: @page, per_page: @per_page, padding: @padding, load: @load, includes: [:include] || [:includes], json: ![:json].nil?, match_suffix: @match_suffix, highlighted_fields: @highlighted_fields || [] } # set execute for multi search @execute = Searchkick::Results.new(searchkick_klass, response, opts) end |
#params ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/searchkick/query.rb', line 53 def params index = if [:index_name] Array([:index_name]).map { |v| v.respond_to?(:searchkick_index) ? v.searchkick_index.name : v }.join(",") elsif searchkick_index searchkick_index.name else "_all" end params = { index: index, body: body } params[:type] = @type if @type params[:routing] = @routing if @routing params.merge!([:request_params]) if [:request_params] params end |
#searchkick_index ⇒ Object
41 42 43 |
# File 'lib/searchkick/query.rb', line 41 def searchkick_index klass ? klass.searchkick_index : nil end |
#searchkick_klass ⇒ Object
49 50 51 |
# File 'lib/searchkick/query.rb', line 49 def searchkick_klass klass ? klass.searchkick_klass : nil end |
#searchkick_options ⇒ Object
45 46 47 |
# File 'lib/searchkick/query.rb', line 45 def klass ? klass. : {} end |
#to_curl ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/searchkick/query.rb', line 88 def to_curl query = params type = query[:type] index = query[:index].is_a?(Array) ? query[:index].join(",") : query[:index] # no easy way to tell which host the client will use host = Searchkick.client.transport.hosts.first credentials = host[:user] || host[:password] ? "#{host[:user]}:#{host[:password]}@" : nil "curl #{host[:protocol]}://#{credentials}#{host[:host]}:#{host[:port]}/#{CGI.escape(index)}#{type ? "/#{type.map { |t| CGI.escape(t) }.join(',')}" : ''}/_search?pretty -d '#{query[:body].to_json}'" end |