Class: Chewy::Search::Parameters
- Inherits:
-
Object
- Object
- Chewy::Search::Parameters
- Defined in:
- lib/chewy/search/parameters.rb,
lib/chewy/search/parameters/knn.rb,
lib/chewy/search/parameters/aggs.rb,
lib/chewy/search/parameters/load.rb,
lib/chewy/search/parameters/none.rb,
lib/chewy/search/parameters/limit.rb,
lib/chewy/search/parameters/order.rb,
lib/chewy/search/parameters/query.rb,
lib/chewy/search/parameters/filter.rb,
lib/chewy/search/parameters/offset.rb,
lib/chewy/search/parameters/source.rb,
lib/chewy/search/parameters/explain.rb,
lib/chewy/search/parameters/indices.rb,
lib/chewy/search/parameters/profile.rb,
lib/chewy/search/parameters/rescore.rb,
lib/chewy/search/parameters/storage.rb,
lib/chewy/search/parameters/suggest.rb,
lib/chewy/search/parameters/timeout.rb,
lib/chewy/search/parameters/version.rb,
lib/chewy/search/parameters/collapse.rb,
lib/chewy/search/parameters/highlight.rb,
lib/chewy/search/parameters/min_score.rb,
lib/chewy/search/parameters/preference.rb,
lib/chewy/search/parameters/post_filter.rb,
lib/chewy/search/parameters/search_type.rb,
lib/chewy/search/parameters/search_after.rb,
lib/chewy/search/parameters/track_scores.rb,
lib/chewy/search/parameters/indices_boost.rb,
lib/chewy/search/parameters/request_cache.rb,
lib/chewy/search/parameters/script_fields.rb,
lib/chewy/search/parameters/stored_fields.rb,
lib/chewy/search/parameters/docvalue_fields.rb,
lib/chewy/search/parameters/terminate_after.rb,
lib/chewy/search/parameters/track_total_hits.rb,
lib/chewy/search/parameters/ignore_unavailable.rb,
lib/chewy/search/parameters/concerns/bool_storage.rb,
lib/chewy/search/parameters/concerns/hash_storage.rb,
lib/chewy/search/parameters/concerns/query_storage.rb,
lib/chewy/search/parameters/concerns/string_storage.rb,
lib/chewy/search/parameters/concerns/integer_storage.rb,
lib/chewy/search/parameters/allow_partial_search_results.rb,
lib/chewy/search/parameters/concerns/string_array_storage.rb
Overview
This class is basically a compound storage of the request parameter storages. It encapsulates some storage-collection-handling logic.
Defined Under Namespace
Modules: BoolStorage, HashStorage, IntegerStorage, QueryStorage, StringArrayStorage, StringStorage Classes: Aggs, AllowPartialSearchResults, Collapse, DocvalueFields, Explain, Filter, Highlight, IgnoreUnavailable, Indices, IndicesBoost, Knn, Limit, Load, MinScore, None, Offset, Order, PostFilter, Preference, Profile, Query, RequestCache, Rescore, ScriptFields, SearchAfter, SearchType, Source, Storage, StoredFields, Suggest, TerminateAfter, Timeout, TrackScores, TrackTotalHits, Version
Constant Summary collapse
- QUERY_STRING_STORAGES =
%i[indices preference search_type request_cache allow_partial_search_results ignore_unavailable].freeze
Instance Attribute Summary collapse
Class Method Summary collapse
-
.storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Default storage classes warehouse.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares storages by their values.
-
#except!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Keeps only specified storages removing everything else.
-
#initialize(initial = {}, **kinitial) ⇒ Parameters
constructor
Accepts an initial hash as basic values or parameter storages.
-
#merge!(other) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Takes all the storages and merges them one by one using Storage#merge! method.
-
#modify!(name) { ... } ⇒ Chewy::Search::Parameters::Storage
Clones the specified storage, performs the operation defined by block on the clone.
-
#only!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Removes specified storages from the storages hash.
-
#render ⇒ Hash
Renders and merges all the parameter storages into a single hash.
Constructor Details
#initialize(initial = {}, **kinitial) ⇒ Parameters
Accepts an initial hash as basic values or parameter storages.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/chewy/search/parameters.rb', line 39 def initialize(initial = {}, **kinitial) @storages = Hash.new do |hash, name| hash[name] = self.class.storages[name].new end initial = initial.deep_dup.merge(kinitial) initial.each_with_object(@storages) do |(name, value), result| storage_class = self.class.storages[name] storage = value.is_a?(storage_class) ? value : storage_class.new(value) result[name] = storage end end |
Instance Attribute Details
#storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
26 27 28 |
# File 'lib/chewy/search/parameters.rb', line 26 def storages @storages end |
Class Method Details
.storages ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Default storage classes warehouse. It is probably possible to add your own classes here if necessary, but I'm not sure it will work.
19 20 21 22 23 |
# File 'lib/chewy/search/parameters.rb', line 19 def self.storages @storages ||= Hash.new do |hash, name| hash[name] = "Chewy::Search::Parameters::#{name.to_s.camelize}".constantize end end |
Instance Method Details
#==(other) ⇒ true, false
Compares storages by their values.
55 56 57 |
# File 'lib/chewy/search/parameters.rb', line 55 def ==(other) super || (other.is_a?(self.class) && compare_storages(other)) end |
#except!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Keeps only specified storages removing everything else.
83 84 85 |
# File 'lib/chewy/search/parameters.rb', line 83 def except!(names) @storages.except!(*assert_storages(names)) end |
#merge!(other) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Takes all the storages and merges them one by one using Chewy::Search::Parameters::Storage#merge! method. Merging is implemented in different ways for different storages: for limit, offset and other single-value classes it is a simple value replacement, for boolean storages (explain, none) it uses a disjunction result, for compound values - merging and concatenation, for query, filter, post_filter - it is the "and" operation.
98 99 100 101 102 |
# File 'lib/chewy/search/parameters.rb', line 98 def merge!(other) other.storages.each do |name, storage| modify!(name) { merge!(storage) } end end |
#modify!(name) { ... } ⇒ Chewy::Search::Parameters::Storage
Clones the specified storage, performs the operation defined by block on the clone.
65 66 67 68 69 |
# File 'lib/chewy/search/parameters.rb', line 65 def modify!(name, &block) @storages[name] = @storages[name].clone.tap do |s| s.instance_exec(&block) end end |
#only!(names) ⇒ {Symbol => Chewy::Search::Parameters::Storage}
Removes specified storages from the storages hash.
75 76 77 |
# File 'lib/chewy/search/parameters.rb', line 75 def only!(names) @storages.slice!(*assert_storages(names)) end |
#render ⇒ Hash
Renders and merges all the parameter storages into a single hash.
107 108 109 |
# File 'lib/chewy/search/parameters.rb', line 107 def render render_query_string_params.merge(render_body) end |