Module: Chewy::Search::Parameters::QueryStorage

Included in:
Filter, PostFilter, Query
Defined in:
lib/chewy/search/parameters/concerns/query_storage.rb

Overview

This is a basic storage implementation for query, filter and post_filter storages. It uses bool query as a root structure for each of them. The bool root is ommited on rendering if there is only a single query in the must or should array. Besides the standard parameter storage capabilities, it provides specialized methods for the bool query component arrays separate update.

Defined Under Namespace

Classes: Bool

Instance Method Summary collapse

Instance Method Details

#and(other_value) ⇒ {Symbol => Array<Hash>}

Unlike #must doesn't modify must array, but joins 2 queries into a single must array of the new root bool query. If any of the used queries is a bool query from the storage and contains a single query in must or should array, it will be reduced to this query, so in some cases it will act exactly the same way as #must.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



144
145
146
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 144

def and(other_value)
  join_into(:must, other_value)
end

#merge!(other) ⇒ {Symbol => Array<Hash>}

Uses and logic to merge storages.

Parameters:

Returns:

See Also:



187
188
189
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 187

def merge!(other)
  self.and(other.value)
end

#minimum_should_match(new_value) ⇒ {Symbol => Array<Hash>}

Replaces minimum_should_match bool query value

Parameters:

  • new_value (String, Integer)

    minimum_should_match value

Returns:

See Also:



177
178
179
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 177

def minimum_should_match(new_value)
  update!(minimum_should_match: new_value)
end

#must(other_value) ⇒ {Symbol => Array<Hash>}

Directly modifies must array of the root bool query. Pushes the passed query to the end of the array.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



110
111
112
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 110

def must(other_value)
  update!(must: other_value)
end

#must_not(other_value) ⇒ {Symbol => Array<Hash>}

Directly modifies must_not array of the root bool query. Pushes the passed query to the end of the array.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



130
131
132
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 130

def must_not(other_value)
  update!(must_not: other_value)
end

#not(other_value) ⇒ {Symbol => Array<Hash>}

Basically, an alias for #must_not.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



168
169
170
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 168

def not(other_value)
  update!(must_not: normalize(other_value).query)
end

#or(other_value) ⇒ {Symbol => Array<Hash>}

Unlike #should doesn't modify should array, but joins 2 queries into a single should array of the new root bool query. If any of the used queries is a bool query from the storage and contains a single query in must or should array, it will be reduced to this query, so in some cases it will act exactly the same way as #should.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



158
159
160
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 158

def or(other_value)
  join_into(:should, other_value)
end

#render{Symbol => Hash}

Almost standard rendering logic, some reduction logic is applied to the value additionally.

Returns:

See Also:



206
207
208
209
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 206

def render
  rendered_bool = value.query
  {self.class.param_name => rendered_bool} if rendered_bool.present?
end

#should(other_value) ⇒ {Symbol => Array<Hash>}

Directly modifies should array of the root bool query. Pushes the passed query to the end of the array.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



120
121
122
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 120

def should(other_value)
  update!(should: other_value)
end

#update!(other_value) ⇒ {Symbol => Array<Hash>}

Every query value is a hash of arrays and each array is glued with the corresponding array from the provided value.

Parameters:

  • other_value (Hash, Array)

    any acceptable storage value

Returns:

See Also:



197
198
199
# File 'lib/chewy/search/parameters/concerns/query_storage.rb', line 197

def update!(other_value)
  @value = value.update(normalize(other_value))
end