Class: Chewy::Search::Parameters

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(initial = {}, **kinitial) ⇒ Parameters

Accepts an initial hash as basic values or parameter storages.

Examples:

Chewy::Search::Parameters.new(limit: 10, offset 10)
Chewy::Search::Parameters.new(
  limit: Chewy::Search::Parameters::Limit.new(10),
  limit: Chewy::Search::Parameters::Offset.new(10)
)

Parameters:



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}

Returns:



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.

Returns:



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.

Parameters:

  • other (Object)

    any object

Returns:

  • (true, false)


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.

Parameters:

  • names (Array<String, Symbol>)

Returns:



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.

Returns:

See Also:



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.

Parameters:

  • name (Symbol)

    parameter name

Yields:

  • the block is executed in the cloned storage instance binding

Returns:



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.

Parameters:

  • names (Array<String, Symbol>)

Returns:



75
76
77
# File 'lib/chewy/search/parameters.rb', line 75

def only!(names)
  @storages.slice!(*assert_storages(names))
end

#renderHash

Renders and merges all the parameter storages into a single hash.

Returns:

  • (Hash)

    request body



107
108
109
# File 'lib/chewy/search/parameters.rb', line 107

def render
  render_query_string_params.merge(render_body)
end