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 omitted 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:

  • ({Symbol => Array<Hash>})

See Also:



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

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

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

Uses and logic to merge storages.

Parameters:

Returns:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Hash})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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:

  • ({Symbol => Array<Hash>})

See Also:



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

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