Class: SearchFlip::Criteria
- Inherits:
-
Object
- Object
- SearchFlip::Criteria
- Extended by:
- Forwardable
- Includes:
- Aggregatable, Filterable, PostFilterable
- 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
-
#custom_value ⇒ Object
Returns the value of attribute custom_value.
-
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
-
#failsafe_value ⇒ Object
Returns the value of attribute failsafe_value.
-
#highlight_values ⇒ Object
Returns the value of attribute highlight_values.
-
#includes_values ⇒ Object
Returns the value of attribute includes_values.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#preload_values ⇒ Object
Returns the value of attribute preload_values.
-
#profile_value ⇒ Object
Returns the value of attribute profile_value.
-
#scroll_args ⇒ Object
Returns the value of attribute scroll_args.
-
#sort_values ⇒ Object
Returns the value of attribute sort_values.
-
#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.
Instance Method Summary collapse
-
#criteria ⇒ SearchFlip::Criteria
private
Convenience method to have a unified conversion api.
-
#custom(hash) ⇒ SearchFlip::Criteria
Adds a fully custom field/section to the request, such that upcoming or minor ElasticSearch features as well as other custom requirements can be used without having yet specialized criteria methods.
-
#delete ⇒ 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(base_url: target.base_url) ⇒ 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_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.
-
#highlight(fields, options = {}) ⇒ SearchFlip::Criteria
Adds highlighting of the given fields to the request.
-
#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.
-
#limit(n) ⇒ SearchFlip::Criteria
Sets the request limit, ie ElasticSearch’s size parameter that is used to restrict the results that get returned.
-
#limit_value_with_default ⇒ Fixnum
Returns the limit value or, if not yet set, the default limit value (30).
-
#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
-
#offset(n) ⇒ SearchFlip::Criteria
Sets the request offset, ie SearchFlip’s from parameter that is used to skip results in the result set from being returned.
-
#offset_value_with_default ⇒ Fixnum
Returns the offset value or, if not yet set, the default limit value (0).
- #page(n) ⇒ Object
-
#paginate(page:, per_page: limit_value_with_default) ⇒ SearchFlip::Criteria
Sets pagination parameters for the criteria by using offset and limit, ie ElasticSearch’s from and size parameters.
- #per(n) ⇒ Object
-
#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.
-
#resort(*args) ⇒ SearchFlip::Criteria
(also: #reorder)
Specify the sort order you want ElasticSearch to use for sorting the results with already existing sort orders being removed.
- #respond_to?(name, *args) ⇒ Boolean
-
#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.
-
#sort(*args) ⇒ SearchFlip::Criteria
(also: #order)
Specify the sort order you want ElasticSearch to use for sorting the results.
-
#source(value) ⇒ SearchFlip::Criteria
Use to specify which fields of the source document you want ElasticSearch to return for each matching result.
-
#suggest(name, options = {}) ⇒ SearchFlip::Criteria
Adds a suggestion section with the given name to the request.
-
#terminate_after(n) ⇒ SearchFlip::Criteria
Specifies early query termination, such that the processing will be stopped after the specified number of results has been accumulated.
-
#timeout(n) ⇒ 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.
-
#unscope(*scopes) ⇒ SearchFlip::Criteria
Creates a new criteria while removing all specified scopes.
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, #must, #must_not, #range, #search, #should, #where, #where_not
Constructor Details
#initialize(attributes = {}) ⇒ Criteria
Creates a new SearchFlip::Criteria.
142 143 144 145 146 |
# File 'lib/search_flip/criteria.rb', line 142 def initialize(attributes = {}) attributes.each do |key, value| self.send "#{key}=", value end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
738 739 740 741 742 743 744 |
# File 'lib/search_flip/criteria.rb', line 738 def method_missing(name, *args, &block) if target.respond_to?(name) merge(target.send(name, *args, &block)) else super end end |
Instance Attribute Details
#custom_value ⇒ Object
Returns the value of attribute custom_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def custom_value @custom_value end |
#eager_load_values ⇒ Object
Returns the value of attribute eager_load_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def eager_load_values @eager_load_values end |
#failsafe_value ⇒ Object
Returns the value of attribute failsafe_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def failsafe_value @failsafe_value end |
#highlight_values ⇒ Object
Returns the value of attribute highlight_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def highlight_values @highlight_values end |
#includes_values ⇒ Object
Returns the value of attribute includes_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def includes_values @includes_values end |
#limit_value ⇒ Object
Returns the value of attribute limit_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def limit_value @limit_value end |
#offset_value ⇒ Object
Returns the value of attribute offset_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def offset_value @offset_value end |
#preload_values ⇒ Object
Returns the value of attribute preload_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def preload_values @preload_values end |
#profile_value ⇒ Object
Returns the value of attribute profile_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def profile_value @profile_value end |
#scroll_args ⇒ Object
Returns the value of attribute scroll_args.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def scroll_args @scroll_args end |
#sort_values ⇒ Object
Returns the value of attribute sort_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def sort_values @sort_values end |
#source_value ⇒ Object
Returns the value of attribute source_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def source_value @source_value end |
#suggest_values ⇒ Object
Returns the value of attribute suggest_values.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def suggest_values @suggest_values end |
#target ⇒ Object
Returns the value of attribute target.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def target @target end |
#terminate_after_value ⇒ Object
Returns the value of attribute terminate_after_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def terminate_after_value @terminate_after_value end |
#timeout_value ⇒ Object
Returns the value of attribute timeout_value.
22 23 24 |
# File 'lib/search_flip/criteria.rb', line 22 def timeout_value @timeout_value end |
Instance Method Details
#criteria ⇒ SearchFlip::Criteria
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.
134 135 136 |
# File 'lib/search_flip/criteria.rb', line 134 def criteria self end |
#custom(hash) ⇒ SearchFlip::Criteria
Use with caution, because using #custom will potentiall override other sections like aggregations
, query
, sort
, etc if you use the the same section names.
Adds a fully custom field/section to the request, such that upcoming or minor ElasticSearch features as well as other custom requirements can be used without having yet specialized criteria methods.
495 496 497 498 499 |
# File 'lib/search_flip/criteria.rb', line 495 def custom(hash) fresh.tap do |criteria| criteria.custom_value = (custom_value || {}).merge(hash) end end |
#delete ⇒ 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.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/search_flip/criteria.rb', line 333 def delete _request = request.dup _request.delete(:from) _request.delete(:size) if SearchFlip.version.to_i >= 5 SearchFlip::HTTPClient.post("#{target.type_url}/_delete_by_query", json: _request) else SearchFlip::HTTPClient.delete("#{target.type_url}/_query", json: _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.
397 398 399 400 401 |
# File 'lib/search_flip/criteria.rb', line 397 def eager_load(*args) fresh.tap do |criteria| criteria.eager_load_values = (eager_load_values || []) + args end end |
#execute(base_url: target.base_url) ⇒ 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 and response errors will be rescued if you specify the criteria to be #failsafe, such that an empty response is returned instead.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/search_flip/criteria.rb', line 667 def execute(base_url: target.base_url) @response ||= begin http_request = SearchFlip::HTTPClient.headers(accept: "application/json") http_response = if scroll_args && scroll_args[:id] if SearchFlip.version.to_i >= 2 http_request.post("#{base_url}/_search/scroll", json: { scroll: scroll_args[:timeout], scroll_id: scroll_args[:id] }) else http_request.headers(content_type: "text/plain").post("#{base_url}/_search/scroll", params: { scroll: scroll_args[:timeout] }, body: scroll_args[:id]) end elsif scroll_args http_request.post("#{target.type_url(base_url: base_url)}/_search", params: { scroll: scroll_args[:timeout] }, json: request) else http_request.post("#{target.type_url(base_url: base_url)}/_search", json: request) end SearchFlip::Response.new(self, http_response.parse) rescue SearchFlip::ConnectionError, SearchFlip::ResponseError => e raise e unless failsafe_value SearchFlip::Response.new(self, "took" => 0, "hits" => { "total" => 0, "hits" => [] }) 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.
713 714 715 716 717 |
# File 'lib/search_flip/criteria.rb', line 713 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.
642 643 644 645 646 647 648 649 650 |
# File 'lib/search_flip/criteria.rb', line 642 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_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.
594 595 596 597 598 599 600 |
# File 'lib/search_flip/criteria.rb', line 594 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.
618 619 620 621 622 623 624 |
# File 'lib/search_flip/criteria.rb', line 618 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.
728 729 730 731 732 |
# File 'lib/search_flip/criteria.rb', line 728 def fresh dup.tap do |criteria| criteria.instance_variable_set(:@response, nil) end end |
#highlight(fields, options = {}) ⇒ SearchFlip::Criteria
Adds highlighting of the given fields to the request.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/search_flip/criteria.rb', line 244 def highlight(fields, = {}) fresh.tap do |criteria| criteria.highlight_values = (criteria.highlight_values || {}).merge() hash = if fields.is_a?(Hash) fields elsif fields.is_a?(Array) fields.each_with_object({}) { |field, h| h[field] = {} } else { fields => {} } end criteria.highlight_values[:fields] = (criteria.highlight_values[:fields] || {}).merge(hash) 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.
378 379 380 381 382 |
# File 'lib/search_flip/criteria.rb', line 378 def includes(*args) fresh.tap do |criteria| criteria.includes_values = (includes_values || []) + args end end |
#limit(n) ⇒ SearchFlip::Criteria
Sets the request limit, ie ElasticSearch’s size parameter that is used to restrict the results that get returned.
537 538 539 540 541 |
# File 'lib/search_flip/criteria.rb', line 537 def limit(n) fresh.tap do |criteria| criteria.limit_value = n.to_i end end |
#limit_value_with_default ⇒ Fixnum
Returns the limit value or, if not yet set, the default limit value (30).
547 548 549 |
# File 'lib/search_flip/criteria.rb', line 547 def limit_value_with_default (limit_value || 30).to_i 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.
37 38 39 40 41 42 43 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 37 def merge(other) other = other.criteria fresh.tap do |criteria| criteria.profile_value = other.profile_value if other.profile_value != nil criteria.source_value = (criteria.source_value || []) + other.source_value if other.source_value criteria.sort_values = (criteria.sort_values || []) + other.sort_values if other.sort_values criteria.highlight_values = (criteria.highlight_values || {}).merge(other.highlight_values) if other.highlight_values criteria.suggest_values = (criteria.suggest_values || {}).merge(other.suggest_values) if other.suggest_values criteria.offset_value = other.offset_value if other.offset_value criteria.limit_value = other.limit_value if other.limit_value criteria.includes_values = (criteria.includes_values || []) + other.includes_values if other.includes_values criteria.preload_values = (criteria.preload_values || []) + other.preload_values if other.preload_values criteria.eager_load_values = (criteria.eager_load_values || []) + other.eager_load_values if other.eager_load_values criteria.failsafe_value = other.failsafe_value if other.failsafe_value != nil criteria.scroll_args = other.scroll_args if other.scroll_args criteria.custom_value = (criteria.custom_value || {}).merge(other.custom_value) if other.custom_value criteria.search_values = (criteria.search_values || []) + other.search_values if other.search_values criteria.must_values = (criteria.must_values || []) + other.must_values if other.must_values criteria.must_not_values = (criteria.must_not_values || []) + other.must_not_values if other.must_not_values criteria.should_values = (criteria.should_values || []) + other.should_values if other.should_values criteria.filter_values = (criteria.filter_values || []) + other.filter_values if other.filter_values criteria.post_search_values = (criteria.post_search_values || []) + other.post_search_values if other.post_search_values criteria.post_must_values = (criteria.post_must_values || []) + other.post_must_values if other.post_must_values criteria.post_must_not_values = (criteria.post_must_not_values || []) + other.post_must_not_values if other.post_must_not_values criteria.post_should_values = (criteria.post_should_values || []) + other.post_should_values if other.post_should_values criteria.post_filter_values = (criteria.post_filter_vales || []) + other.post_filter_values if other.post_filter_values criteria.aggregation_values = (criteria.aggregation_values || {}).merge(other.aggregation_values) if other.aggregation_values criteria.terminate_after_value = other.terminate_after_value if other.terminate_after_value != nil criteria.timeout_value = other.timeout_value if other.timeout_value != nil end end |
#offset(n) ⇒ SearchFlip::Criteria
Sets the request offset, ie SearchFlip’s from parameter that is used to skip results in the result set from being returned.
512 513 514 515 516 |
# File 'lib/search_flip/criteria.rb', line 512 def offset(n) fresh.tap do |criteria| criteria.offset_value = n.to_i end end |
#offset_value_with_default ⇒ Fixnum
Returns the offset value or, if not yet set, the default limit value (0).
522 523 524 |
# File 'lib/search_flip/criteria.rb', line 522 def offset_value_with_default (offset_value || 0).to_i end |
#page(n) ⇒ Object
570 571 572 |
# File 'lib/search_flip/criteria.rb', line 570 def page(n) paginate(page: n) end |
#paginate(page:, per_page: limit_value_with_default) ⇒ SearchFlip::Criteria
Sets pagination parameters for the criteria by using offset and limit, ie ElasticSearch’s from and size parameters.
563 564 565 566 567 568 |
# File 'lib/search_flip/criteria.rb', line 563 def paginate(page:, per_page: limit_value_with_default) page = [page.to_i, 1].max per_page = per_page.to_i offset((page - 1) * per_page).limit(per_page) end |
#per(n) ⇒ Object
574 575 576 |
# File 'lib/search_flip/criteria.rb', line 574 def per(n) paginate(page: offset_value_with_default / limit_value_with_default + 1, per_page: n) 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.
416 417 418 419 420 |
# File 'lib/search_flip/criteria.rb', line 416 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.
289 290 291 292 293 |
# File 'lib/search_flip/criteria.rb', line 289 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.
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/search_flip/criteria.rb', line 154 def request res = {} if must_values || search_values || must_not_values || should_values || filter_values if SearchFlip.version.to_i >= 2 res[:query] = { bool: {}. merge(must_values || search_values ? { must: (must_values || []) + (search_values || [])} : {}). merge(must_not_values ? { must_not: must_not_values } : {}). merge(should_values ? { should: should_values } : {}). merge(filter_values ? { filter: filter_values } : {}) } else filters = (filter_values || []) + (must_not_values || []).map { |must_not_value| { not: must_not_value } } queries = {}. merge(must_values || search_values ? { must: (must_values || []) + (search_values || []) } : {}). merge(should_values ? { should: should_values } : {}) if filters.size > 0 res[:query] = { filtered: {}. merge(queries.size > 0 ? { query: { bool: queries } } : {}). merge(filter: filters.size > 1 ? { and: filters } : filters.first) } else res[:query] = { bool: queries } end end end res.update from: offset_value_with_default, size: limit_value_with_default 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_search_values || post_must_not_values || post_should_values || post_filter_values if SearchFlip.version.to_i >= 2 res[:post_filter] = { bool: {}. merge(post_must_values || post_search_values ? { must: (post_must_values || []) + (post_search_values || []) } : {}). merge(post_must_not_values ? { must_not: post_must_not_values } : {}). merge(post_should_values ? { should: post_should_values } : {}). merge(post_filter_values ? { filter: post_filter_values } : {}) } else post_filters = (post_filter_values || []) + (post_must_not_values || []).map { |post_must_not_value| { not: post_must_not_value } } post_queries = {}. merge(post_must_values || post_search_values ? { must: (post_must_values || []) + (post_search_values || []) } : {}). merge(post_should_values ? { should: post_should_values } : {}) post_filters_and_queries = post_filters + (post_queries.size > 0 ? [bool: post_queries] : []) res[:post_filter] = post_filters_and_queries.size > 1 ? { and: post_filters_and_queries } : post_filters_and_queries.first end end res[:_source] = source_value unless source_value.nil? res[:profile] = true if profile_value res.update(custom_value) if custom_value res end |
#resort(*args) ⇒ SearchFlip::Criteria Also known as: reorder
Specify the sort order you want ElasticSearch to use for sorting the results with already existing sort orders being removed.
471 472 473 474 475 |
# File 'lib/search_flip/criteria.rb', line 471 def resort(*args) fresh.tap do |criteria| criteria.sort_values = args end end |
#respond_to?(name, *args) ⇒ Boolean
734 735 736 |
# File 'lib/search_flip/criteria.rb', line 734 def respond_to?(name, *args) super || target.respond_to?(name, *args) 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.
315 316 317 318 319 |
# File 'lib/search_flip/criteria.rb', line 315 def scroll(id: nil, timeout: "1m") fresh.tap do |criteria| criteria.scroll_args = { id: id, timeout: timeout } end end |
#sort(*args) ⇒ SearchFlip::Criteria Also known as: order
Specify the sort order you want ElasticSearch to use for sorting the results. When you call this multiple times, the sort orders are appended to the already existing ones. The sort arguments get passed to ElasticSearch without modifications, such that you can use sort by script, etc here as well.
449 450 451 452 453 |
# File 'lib/search_flip/criteria.rb', line 449 def sort(*args) fresh.tap do |criteria| criteria.sort_values = (sort_values || []) + args end end |
#source(value) ⇒ SearchFlip::Criteria
Use to specify which fields of the source document you want ElasticSearch to return for each matching result.
359 360 361 362 363 |
# File 'lib/search_flip/criteria.rb', line 359 def source(value) fresh.tap do |criteria| criteria.source_value = value end end |
#suggest(name, options = {}) ⇒ SearchFlip::Criteria
Adds a suggestion section with the given name to the request.
273 274 275 276 277 |
# File 'lib/search_flip/criteria.rb', line 273 def suggest(name, = {}) fresh.tap do |criteria| criteria.suggest_values = (criteria.suggest_values || {}).merge(name => ) end end |
#terminate_after(n) ⇒ SearchFlip::Criteria
Specifies early query termination, such that the processing will be stopped after the specified number of results has been accumulated.
93 94 95 96 97 |
# File 'lib/search_flip/criteria.rb', line 93 def terminate_after(n) fresh.tap do |criteria| criteria.terminate_after_value = n end end |
#timeout(n) ⇒ 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.
79 80 81 82 83 |
# File 'lib/search_flip/criteria.rb', line 79 def timeout(n) fresh.tap do |criteria| criteria.timeout_value = n end end |
#unscope(*scopes) ⇒ SearchFlip::Criteria
Creates a new criteria while removing all specified scopes. Currently, you can unscope :search, :post_search, :sort, :highlight, :suggest, :custom and :aggregate.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/search_flip/criteria.rb', line 110 def unscope(*scopes) unknown = scopes - [:search, :post_search, :sort, :highlight, :suggest, :custom, :aggregate] raise(ArgumentError, "Can't unscope #{unknown.join(", ")}") if unknown.size > 0 scopes = scopes.to_set fresh.tap do |criteria| criteria.search_values = nil if scopes.include?(:search) criteria.post_search_values = nil if scopes.include?(:search) criteria.sort_values = nil if scopes.include?(:sort) criteria.hightlight_values = nil if scopes.include?(:highlight) criteria.suggest_values = nil if scopes.include?(:suggest) criteria.custom_values = nil if scopes.include?(:custom) criteria.aggregation_values = nil if scopes.include?(:aggregate) end end |