Class: Searchkick::Query
- Inherits:
-
Object
- Object
- Searchkick::Query
- Extended by:
- Forwardable
- Includes:
- QueryWithInstrumentation
- Defined in:
- lib/searchkick/query.rb
Constant Summary collapse
- @@metric_aggs =
[:avg, :cardinality, :max, :min, :sum]
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
- #retry_misspellings?(response) ⇒ Boolean
- #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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/searchkick/query.rb', line 17 def initialize(klass, term = "*", **) unknown_keywords = .keys - [:aggs, :body, :body_options, :boost, :boost_by, :boost_by_distance, :boost_where, :conversions, :conversions_term, :debug, :emoji, :exclude, :execute, :explain, :fields, :highlight, :includes, :index_name, :indices_boost, :limit, :load, :match, :misspellings, :model_includes, :offset, :operator, :order, :padding, :page, :per_page, :profile, :request_params, :routing, :scope_results, :select, :similar, :smart_aggs, :suggest, :track, :type, :where] raise ArgumentError, "unknown keywords: #{unknown_keywords.join(", ")}" if unknown_keywords.any? term = term.to_s 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 = false @misspellings_below = nil @highlighted_fields = nil prepare end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/searchkick/query.rb', line 8 def body @body end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
7 8 9 |
# File 'lib/searchkick/query.rb', line 7 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/searchkick/query.rb', line 7 def @options end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
7 8 9 |
# File 'lib/searchkick/query.rb', line 7 def term @term end |
Instance Method Details
#execute ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/searchkick/query.rb', line 78 def execute @execute ||= begin begin response = execute_search if retry_misspellings?(response) prepare response = execute_search end rescue => e # TODO rescue type handle_error(e) end handle_response(response) end end |
#handle_response(response) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/searchkick/query.rb', line 104 def handle_response(response) opts = { page: @page, per_page: @per_page, padding: @padding, load: @load, includes: [:includes], model_includes: [:model_includes], json: !@json.nil?, match_suffix: @match_suffix, highlighted_fields: @highlighted_fields || [], misspellings: @misspellings, term: term, scope_results: [:scope_results], index_name: [:index_name] } if [:debug] require "pp" puts "Searchkick Version: #{Searchkick::VERSION}" puts "Elasticsearch Version: #{Searchkick.server_version}" puts puts "Model Searchkick Options" pp puts puts "Search Options" pp puts if searchkick_index puts "Model Search Data" begin pp klass.limit(3).map { |r| RecordData.new(searchkick_index, r).index_data } rescue => e puts "#{e.class.name}: #{e.}" end puts puts "Elasticsearch Mapping" puts JSON.pretty_generate(searchkick_index.mapping) puts puts "Elasticsearch Settings" puts JSON.pretty_generate(searchkick_index.settings) puts end puts "Elasticsearch Query" puts to_curl puts puts "Elasticsearch Results" puts JSON.pretty_generate(response) end # set execute for multi search @execute = Searchkick::Results.new(searchkick_klass, response, opts) end |
#params ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/searchkick/query.rb', line 58 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 |
#retry_misspellings?(response) ⇒ Boolean
166 167 168 |
# File 'lib/searchkick/query.rb', line 166 def retry_misspellings?(response) @misspellings_below && response["hits"]["total"] < @misspellings_below end |
#searchkick_index ⇒ Object
46 47 48 |
# File 'lib/searchkick/query.rb', line 46 def searchkick_index klass ? klass.searchkick_index : nil end |
#searchkick_klass ⇒ Object
54 55 56 |
# File 'lib/searchkick/query.rb', line 54 def searchkick_klass klass ? klass.searchkick_klass : nil end |
#searchkick_options ⇒ Object
50 51 52 |
# File 'lib/searchkick/query.rb', line 50 def klass ? klass. : {} end |
#to_curl ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/searchkick/query.rb', line 93 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 -H 'Content-Type: application/json' -d '#{query[:body].to_json}'" end |