Class: SearchFlip::Criteria
- Inherits:
-
Object
- Object
- SearchFlip::Criteria
- Extended by:
- Forwardable
- Includes:
- Aggregatable, Customable, Explainable, Filterable, Highlightable, Paginatable, PostFilterable, Sortable, Sourceable
- Defined in:
- lib/search_flip/criteria.rb
Overview
The SearchFlip::Criteria class serves the purpose of chaining various filtering and aggregation methods. Each chainable method creates a new criteria object until a method is called that finally sends the respective request to Elasticsearch and returns the result.
Instance Attribute Summary collapse
-
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
-
#failsafe_value ⇒ Object
Returns the value of attribute failsafe_value.
-
#http_timeout_value ⇒ Object
Returns the value of attribute http_timeout_value.
-
#includes_values ⇒ Object
Returns the value of attribute includes_values.
-
#preference_value ⇒ Object
Returns the value of attribute preference_value.
-
#preload_values ⇒ Object
Returns the value of attribute preload_values.
-
#profile_value ⇒ Object
Returns the value of attribute profile_value.
-
#routing_value ⇒ Object
Returns the value of attribute routing_value.
-
#scroll_args ⇒ Object
Returns the value of attribute scroll_args.
-
#search_type_value ⇒ Object
Returns the value of attribute search_type_value.
-
#source_value ⇒ Object
Returns the value of attribute source_value.
-
#suggest_values ⇒ Object
Returns the value of attribute suggest_values.
-
#target ⇒ Object
Returns the value of attribute target.
-
#terminate_after_value ⇒ Object
Returns the value of attribute terminate_after_value.
-
#timeout_value ⇒ Object
Returns the value of attribute timeout_value.
-
#track_total_hits_value ⇒ Object
Returns the value of attribute track_total_hits_value.
Instance Method Summary collapse
-
#criteria ⇒ SearchFlip::Criteria
(also: #all)
private
Convenience method to have a unified conversion api.
-
#delete(params = {}) ⇒ Object
Sends a delete by query request to Elasticsearch, such that all documents matching the query get deleted.
-
#eager_load(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to eager load via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
-
#execute ⇒ SearchFlip::Response
(also: #response)
Executes the search request for the current criteria, ie sends the request to Elasticsearch and returns the response.
-
#failsafe(value) ⇒ SearchFlip::Response
Marks the criteria to be failsafe, ie certain exceptions raised due to invalid queries, inavailability of Elasticsearch, etc get rescued and an empty criteria is returned instead.
-
#find_each(options = {}) ⇒ Object
(also: #each)
Fetches the records specified by the relatin in batches using the Elasticsearch scroll API and yields each record.
-
#find_each_result(options = {}) ⇒ Object
Fetches the results specified by the criteria in batches using the Elasticsearch scroll API and yields each result.
-
#find_in_batches(options = {}) ⇒ Object
Fetches the records specified by the criteria in batches using the ElasicSearch scroll API and yields each batch.
-
#find_results_in_batches(options = {}) ⇒ Object
Fetches the results specified by the criteria in batches using the Elasticsearch scroll API and yields each batch.
-
#fresh ⇒ SearchFlip::Response
Returns a fresh, ie dupped, criteria with the response cache being cleared.
-
#http_timeout(value) ⇒ SearchFlip::Criteria
Specifies a http timeout, such that a SearchFlip::TimeoutError will be thrown when the request times out.
-
#includes(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to include via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
-
#initialize(attributes = {}) ⇒ Criteria
constructor
Creates a new SearchFlip::Criteria.
-
#merge(other) ⇒ SearchFlip::Criteria
Creates a new criteria while merging the attributes (constraints, settings, etc) of the current criteria with the attributes of another one passed as argument.
- #method_missing(name, *args, &block) ⇒ Object
-
#preference(value) ⇒ SearchFlip::Criteria
Specifies a preference value for the request.
-
#preload(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to preload via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
-
#profile(value) ⇒ SearchFlip::Criteria
Sets whether or not query profiling should be enabled.
-
#request ⇒ Hash
Generates the request object from the attributes specified via chaining, like eg offset, limit, query, filters, aggregations, etc and returns a Hash that later gets serialized as JSON.
- #respond_to_missing?(name, *args) ⇒ Boolean
-
#routing(value) ⇒ SearchFlip::Criteria
Specifies the routing value for the request.
-
#scroll(id: nil, timeout: "1m") ⇒ SearchFlip::Criteria
Adds scrolling to the request with or without an already existing scroll id and using the specified timeout.
-
#search_type(value) ⇒ SearchFlip::Criteria
Specifies the search type value for the request.
-
#suggest(name, options = {}) ⇒ SearchFlip::Criteria
Adds a suggestion section with the given name to the request.
-
#terminate_after(value) ⇒ SearchFlip::Criteria
Specifies early query termination, such that the processing will be stopped after the specified number of results has been accumulated.
-
#timeout(value) ⇒ SearchFlip::Criteria
Specifies a query timeout, such that the processing will be stopped after that timeout and only the results calculated up to that point will be processed and returned.
-
#track_total_hits(value) ⇒ SearchFlip::Criteria
Specifies if or how many hits should be counted/tracked.
-
#with_settings ⇒ SearchFlip::Criteria
Allows to set query specific settings like e.g.
Methods included from Aggregatable
Methods included from PostFilterable
included, #post_exists, #post_exists_not, #post_filter, #post_must, #post_must_not, #post_range, #post_search, #post_should, #post_where, #post_where_not
Methods included from Filterable
#exists, #exists_not, #filter, included, #match_all, #match_none, #must, #must_not, #range, #search, #should, #to_query, #where, #where_not
Methods included from Customable
Methods included from Paginatable
included, #limit, #limit_value_with_default, #offset, #offset_value_with_default, #page, #paginate, #per
Methods included from Explainable
Methods included from Highlightable
Methods included from Sourceable
Methods included from Sortable
Constructor Details
#initialize(attributes = {}) ⇒ Criteria
Creates a new SearchFlip::Criteria.
200 201 202 203 204 |
# File 'lib/search_flip/criteria.rb', line 200 def initialize(attributes = {}) attributes.each do |key, value| send "#{key}=", value end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
594 595 596 597 598 599 600 |
# File 'lib/search_flip/criteria.rb', line 594 def method_missing(name, *args, &block) if target.respond_to?(name) merge(target.send(name, *args, &block)) else super end end |
Instance Attribute Details
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def eager_load_values @eager_load_values end |
#failsafe_value ⇒ Object
Returns the value of attribute failsafe_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def failsafe_value @failsafe_value end |
#http_timeout_value ⇒ Object
Returns the value of attribute http_timeout_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def http_timeout_value @http_timeout_value end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def includes_values @includes_values end |
#preference_value ⇒ Object
Returns the value of attribute preference_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def preference_value @preference_value end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def preload_values @preload_values end |
#profile_value ⇒ Object
Returns the value of attribute profile_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def profile_value @profile_value end |
#routing_value ⇒ Object
Returns the value of attribute routing_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def routing_value @routing_value end |
#scroll_args ⇒ Object
Returns the value of attribute scroll_args.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def scroll_args @scroll_args end |
#search_type_value ⇒ Object
Returns the value of attribute search_type_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def search_type_value @search_type_value end |
#source_value ⇒ Object
Returns the value of attribute source_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def source_value @source_value end |
#suggest_values ⇒ Object
Returns the value of attribute suggest_values.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def suggest_values @suggest_values end |
#target ⇒ Object
Returns the value of attribute target.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def target @target end |
#terminate_after_value ⇒ Object
Returns the value of attribute terminate_after_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def terminate_after_value @terminate_after_value end |
#timeout_value ⇒ Object
Returns the value of attribute timeout_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def timeout_value @timeout_value end |
#track_total_hits_value ⇒ Object
Returns the value of attribute track_total_hits_value.
27 28 29 |
# File 'lib/search_flip/criteria.rb', line 27 def track_total_hits_value @track_total_hits_value end |
Instance Method Details
#criteria ⇒ SearchFlip::Criteria Also known as: all
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convenience method to have a unified conversion api.
190 191 192 |
# File 'lib/search_flip/criteria.rb', line 190 def criteria self end |
#delete(params = {}) ⇒ Object
Sends a delete by query request to Elasticsearch, such that all documents matching the query get deleted. Please note, for certain Elasticsearch versions you need to install the delete-by-query plugin to get support for this feature. Refreshes the index if the auto_refresh is enabled. Raises SearchFlip::ResponseError in case any errors occur.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/search_flip/criteria.rb', line 345 def delete(params = {}) dupped_request = request.dup dupped_request.delete(:from) dupped_request.delete(:size) http_request = connection.http_client http_request = http_request.timeout(http_timeout_value) if http_timeout_value if connection.distribution || connection.version.to_i >= 5 url = connection.distribution.nil? && connection.version.to_i < 8 ? target.type_url : target.index_url http_request.post("#{url}/_delete_by_query", params: request_params.merge(params), json: dupped_request) else http_request.delete("#{target.type_url}/_query", params: request_params.merge(params), json: dupped_request) end target.refresh if SearchFlip::Config[:auto_refresh] true end |
#eager_load(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to eager load via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
398 399 400 401 402 |
# File 'lib/search_flip/criteria.rb', line 398 def eager_load(*args) fresh.tap do |criteria| criteria.eager_load_values = (eager_load_values || []) + args end end |
#execute ⇒ SearchFlip::Response Also known as: response
Executes the search request for the current criteria, ie sends the request to Elasticsearch and returns the response. Connection, timeout and response errors will be rescued if you specify the criteria to be #failsafe, such that an empty response is returned instead.
535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/search_flip/criteria.rb', line 535 def execute @response ||= begin Config[:instrumenter].instrument("request.search_flip", index: target, request: request) do |payload| response = execute! payload[:response] = response response end end end |
#failsafe(value) ⇒ SearchFlip::Response
Marks the criteria to be failsafe, ie certain exceptions raised due to invalid queries, inavailability of Elasticsearch, etc get rescued and an empty criteria is returned instead.
568 569 570 571 572 |
# File 'lib/search_flip/criteria.rb', line 568 def failsafe(value) fresh.tap do |criteria| criteria.failsafe_value = value end end |
#find_each(options = {}) ⇒ Object Also known as: each
Fetches the records specified by the relatin in batches using the Elasticsearch scroll API and yields each record. The batch size and scroll API timeout can be specified. Check out the Elasticsearch docs for further details.
487 488 489 490 491 492 493 494 495 |
# File 'lib/search_flip/criteria.rb', line 487 def find_each( = {}) return enum_for(:find_each, ) unless block_given? find_in_batches do |batch| batch.each do |record| yield record end end end |
#find_each_result(options = {}) ⇒ Object
Fetches the results specified by the criteria in batches using the Elasticsearch scroll API and yields each result. The batch size and scroll API timeout can be specified. Checkout out the Elasticsearch docs for further details.
515 516 517 518 519 520 521 522 523 |
# File 'lib/search_flip/criteria.rb', line 515 def find_each_result( = {}) return enum_for(:find_each_result, ) unless block_given? find_results_in_batches do |batch| batch.each do |result| yield result end end end |
#find_in_batches(options = {}) ⇒ Object
Fetches the records specified by the criteria in batches using the ElasicSearch scroll API and yields each batch. The batch size and scroll API timeout can be specified. Check out the Elasticsearch docs for further details.
439 440 441 442 443 444 445 |
# File 'lib/search_flip/criteria.rb', line 439 def find_in_batches( = {}) return enum_for(:find_in_batches, ) unless block_given? yield_in_batches() do |criteria| yield(criteria.records) if criteria.records.size > 0 end end |
#find_results_in_batches(options = {}) ⇒ Object
Fetches the results specified by the criteria in batches using the Elasticsearch scroll API and yields each batch. The batch size and scroll API timeout can be specified. Checkout out the Elasticsearch docs for further details.
463 464 465 466 467 468 469 |
# File 'lib/search_flip/criteria.rb', line 463 def find_results_in_batches( = {}) return enum_for(:find_results_in_batches, ) unless block_given? yield_in_batches() do |criteria| yield criteria.results end end |
#fresh ⇒ SearchFlip::Response
Returns a fresh, ie dupped, criteria with the response cache being cleared.
583 584 585 586 587 588 |
# File 'lib/search_flip/criteria.rb', line 583 def fresh dup.tap do |criteria| criteria.instance_variable_set(:@request, nil) criteria.instance_variable_set(:@response, nil) end end |
#http_timeout(value) ⇒ SearchFlip::Criteria
Specifies a http timeout, such that a SearchFlip::TimeoutError will be thrown when the request times out.
162 163 164 165 166 |
# File 'lib/search_flip/criteria.rb', line 162 def http_timeout(value) fresh.tap do |criteria| criteria.http_timeout_value = value end end |
#includes(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to include via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
379 380 381 382 383 |
# File 'lib/search_flip/criteria.rb', line 379 def includes(*args) fresh.tap do |criteria| criteria.includes_values = (includes_values || []) + args end end |
#merge(other) ⇒ SearchFlip::Criteria
Creates a new criteria while merging the attributes (constraints, settings, etc) of the current criteria with the attributes of another one passed as argument. For multi-value contstraints the resulting criteria will include constraints of both criterias. For single-value constraints, the values of the criteria passed as an argument are used.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/search_flip/criteria.rb', line 44 def merge(other) other = other.criteria fresh.tap do |criteria| [ :profile_value, :failsafe_value, :terminate_after_value, :timeout_value, :offset_value, :limit_value, :scroll_args, :source_value, :preference_value, :search_type_value, :routing_value, :track_total_hits_value, :explain_value, :http_timeout_value ].each do |name| criteria.send(:"#{name}=", other.send(name)) unless other.send(name).nil? end [ :sort_values, :includes_values, :preload_values, :eager_load_values, :must_values, :must_not_values, :filter_values, :post_must_values, :post_must_not_values, :post_filter_values ].each do |name| criteria.send(:"#{name}=", (criteria.send(name) || []) + other.send(name)) if other.send(name) end [:highlight_values, :suggest_values, :custom_value, :aggregation_values].each do |name| criteria.send(:"#{name}=", (criteria.send(name) || {}).merge(other.send(name))) if other.send(name) end end end |
#preference(value) ⇒ SearchFlip::Criteria
Specifies a preference value for the request. Check out the elasticsearch docs for further details.
97 98 99 100 101 |
# File 'lib/search_flip/criteria.rb', line 97 def preference(value) fresh.tap do |criteria| criteria.preference_value = value end end |
#preload(*args) ⇒ SearchFlip::Criteria
Specify associations of the target model you want to preload via ActiveRecord’s or other ORM’s mechanisms when records get fetched from the database.
417 418 419 420 421 |
# File 'lib/search_flip/criteria.rb', line 417 def preload(*args) fresh.tap do |criteria| criteria.preload_values = (preload_values || []) + args end end |
#profile(value) ⇒ SearchFlip::Criteria
Sets whether or not query profiling should be enabled.
301 302 303 304 305 |
# File 'lib/search_flip/criteria.rb', line 301 def profile(value) fresh.tap do |criteria| criteria.profile_value = value end end |
#request ⇒ Hash
Generates the request object from the attributes specified via chaining, like eg offset, limit, query, filters, aggregations, etc and returns a Hash that later gets serialized as JSON.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/search_flip/criteria.rb', line 228 def request @request ||= begin res = {} if must_values || must_not_values || filter_values res[:query] = { bool: { must: must_values.to_a, must_not: must_not_values.to_a, filter: filter_values.to_a }.reject { |_, value| value.empty? } } end res.update(from: offset_value_with_default, size: limit_value_with_default) res[:track_total_hits] = track_total_hits_value unless track_total_hits_value.nil? res[:explain] = explain_value unless explain_value.nil? res[:timeout] = timeout_value if timeout_value res[:terminate_after] = terminate_after_value if terminate_after_value res[:highlight] = highlight_values if highlight_values res[:suggest] = suggest_values if suggest_values res[:sort] = sort_values if sort_values res[:aggregations] = aggregation_values if aggregation_values if post_must_values || post_must_not_values || post_filter_values res[:post_filter] = { bool: { must: post_must_values.to_a, must_not: post_must_not_values.to_a, filter: post_filter_values.to_a }.reject { |_, value| value.empty? } } end res[:_source] = source_value unless source_value.nil? res[:profile] = true if profile_value res.update(custom_value) if custom_value res end end |
#respond_to_missing?(name, *args) ⇒ Boolean
590 591 592 |
# File 'lib/search_flip/criteria.rb', line 590 def respond_to_missing?(name, *args) target.respond_to?(name, *args) || super end |
#routing(value) ⇒ SearchFlip::Criteria
Specifies the routing value for the request. Check out the elasticsearch docs for further details.
129 130 131 132 133 |
# File 'lib/search_flip/criteria.rb', line 129 def routing(value) fresh.tap do |criteria| criteria.routing_value = value end end |
#scroll(id: nil, timeout: "1m") ⇒ SearchFlip::Criteria
Adds scrolling to the request with or without an already existing scroll id and using the specified timeout.
327 328 329 330 331 |
# File 'lib/search_flip/criteria.rb', line 327 def scroll(id: nil, timeout: "1m") fresh.tap do |criteria| criteria.scroll_args = { id: id, timeout: timeout } end end |
#search_type(value) ⇒ SearchFlip::Criteria
Specifies the search type value for the request. Check out the elasticsearch docs for further details.
113 114 115 116 117 |
# File 'lib/search_flip/criteria.rb', line 113 def search_type(value) fresh.tap do |criteria| criteria.search_type_value = value end end |
#suggest(name, options = {}) ⇒ SearchFlip::Criteria
Adds a suggestion section with the given name to the request.
285 286 287 288 289 |
# File 'lib/search_flip/criteria.rb', line 285 def suggest(name, = {}) fresh.tap do |criteria| criteria.suggest_values = (criteria.suggest_values || {}).merge(name => ) end end |
#terminate_after(value) ⇒ SearchFlip::Criteria
Specifies early query termination, such that the processing will be stopped after the specified number of results has been accumulated.
178 179 180 181 182 |
# File 'lib/search_flip/criteria.rb', line 178 def terminate_after(value) fresh.tap do |criteria| criteria.terminate_after_value = value end end |
#timeout(value) ⇒ SearchFlip::Criteria
Specifies a query timeout, such that the processing will be stopped after that timeout and only the results calculated up to that point will be processed and returned.
146 147 148 149 150 |
# File 'lib/search_flip/criteria.rb', line 146 def timeout(value) fresh.tap do |criteria| criteria.timeout_value = value end end |
#track_total_hits(value) ⇒ SearchFlip::Criteria
Specifies if or how many hits should be counted/tracked. Check out the elasticsearch docs for futher details.
81 82 83 84 85 |
# File 'lib/search_flip/criteria.rb', line 81 def track_total_hits(value) fresh.tap do |criteria| criteria.track_total_hits_value = value end end |
#with_settings ⇒ SearchFlip::Criteria
Allows to set query specific settings like e.g. connection and index name. Please note, however, that this should only be used for special cases and the subsequent query can not be serialized. Checkout SearchFlip::Index.with_settings for more details.
216 217 218 219 220 |
# File 'lib/search_flip/criteria.rb', line 216 ruby2_keywords def with_settings(*args) fresh.tap do |criteria| criteria.target = target.with_settings(*args) end end |