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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/searchkick/query.rb', line 16 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" 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/searchkick/query.rb', line 67 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/searchkick/query.rb', line 93 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/searchkick/query.rb', line 48 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.merge!(type: @type) if @type params.merge!(routing: @routing) if @routing params end |
#searchkick_index ⇒ Object
36 37 38 |
# File 'lib/searchkick/query.rb', line 36 def searchkick_index klass ? klass.searchkick_index : nil end |
#searchkick_klass ⇒ Object
44 45 46 |
# File 'lib/searchkick/query.rb', line 44 def searchkick_klass klass ? klass.searchkick_klass : nil end |
#searchkick_options ⇒ Object
40 41 42 |
# File 'lib/searchkick/query.rb', line 40 def klass ? klass. : {} end |
#to_curl ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/searchkick/query.rb', line 82 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 |