Class: Searchkick::Query
- Inherits:
-
Object
- Object
- Searchkick::Query
- Extended by:
- Forwardable
- Includes:
- Enumerable
- 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
Constructor Details
#initialize(klass, term = "*", **options) ⇒ Query
Returns a new instance of Query.
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 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/searchkick/query.rb', line 19 def initialize(klass, term = "*", **) if [:conversions] Searchkick.warn("The `conversions` option is deprecated in favor of `conversions_v2`, which provides much better search performance. Upgrade to `conversions_v2` or rename `conversions` to `conversions_v1`") end if .key?(:conversions_v1) [:conversions] = .delete(:conversions_v1) end unknown_keywords = .keys - [:aggs, :block, :body, :body_options, :boost, :boost_by, :boost_by_distance, :boost_by_recency, :boost_where, :conversions, :conversions_v2, :conversions_term, :debug, :emoji, :exclude, :explain, :fields, :highlight, :includes, :index_name, :indices_boost, :knn, :limit, :load, :match, :misspellings, :models, :model_includes, :offset, :opaque_id, :operator, :order, :padding, :page, :per_page, :profile, :request_params, :routing, :scope_results, :scroll, :select, :similar, :smart_aggs, :suggest, :total_entries, :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.tr('_', ' ')} " }.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 @index_mapping = nil prepare end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
9 10 11 |
# File 'lib/searchkick/query.rb', line 9 def body @body end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/searchkick/query.rb', line 8 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/searchkick/query.rb', line 8 def @options end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
8 9 10 |
# File 'lib/searchkick/query.rb', line 8 def term @term end |
Instance Method Details
#execute ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/searchkick/query.rb', line 102 def execute @execute ||= begin begin response = execute_search if retry_misspellings?(response) prepare response = execute_search end rescue => e handle_error(e) end handle_response(response) end end |
#handle_response(response) ⇒ Object
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/searchkick/query.rb', line 117 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, highlight: [:highlight], highlighted_fields: @highlighted_fields || [], misspellings: @misspellings, term: term, scope_results: [:scope_results], total_entries: [:total_entries], index_mapping: @index_mapping, suggest: [:suggest], scroll: [:scroll], opaque_id: [:opaque_id] } if [:debug] server = Searchkick.opensearch? ? "OpenSearch" : "Elasticsearch" puts "Searchkick #{Searchkick::VERSION}" puts "#{server} #{Searchkick.server_version}" puts puts "Model Options" pp puts puts "Search Options" pp puts if searchkick_index puts "Record 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 "Mapping" puts JSON.pretty_generate(searchkick_index.mapping) puts puts "Settings" puts JSON.pretty_generate(searchkick_index.settings) puts end puts "Query" puts JSON.pretty_generate(params[:body]) puts puts "Results" puts JSON.pretty_generate(response.to_h) end # set execute for multi search @execute = Results.new(searchkick_klass, response, opts) end |
#params ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/searchkick/query.rb', line 69 def params if [:models] @index_mapping = {} Array([:models]).each do |model| # there can be multiple models per index name due to inheritance - see #1259 (@index_mapping[model.searchkick_index.name] ||= []) << model end end index = if [:index_name] Array([:index_name]).map { |v| v.respond_to?(:searchkick_index) ? v.searchkick_index.name : v }.join(",") elsif [:models] @index_mapping.keys.join(",") elsif searchkick_index searchkick_index.name else # fixes warning about accessing system indices "*,-.*" end params = { index: index, body: body } params[:type] = @type if @type params[:routing] = @routing if @routing params[:scroll] = @scroll if @scroll params[:opaque_id] = @opaque_id if @opaque_id params.merge!([:request_params]) if [:request_params] params end |
#retry_misspellings?(response) ⇒ Boolean
183 184 185 |
# File 'lib/searchkick/query.rb', line 183 def retry_misspellings?(response) @misspellings_below && response["error"].nil? && Results.new(searchkick_klass, response).total_count < @misspellings_below end |
#searchkick_index ⇒ Object
57 58 59 |
# File 'lib/searchkick/query.rb', line 57 def searchkick_index klass ? klass.searchkick_index : nil end |
#searchkick_klass ⇒ Object
65 66 67 |
# File 'lib/searchkick/query.rb', line 65 def searchkick_klass klass ? klass.searchkick_klass : nil end |
#searchkick_options ⇒ Object
61 62 63 |
# File 'lib/searchkick/query.rb', line 61 def klass ? klass. : {} end |