Method: SearchFlip::Filterable#where

Defined in:
lib/search_flip/filterable.rb

#where(hash) ⇒ SearchFlip::Criteria

Adds filters to your criteria for the supplied hash composed of field-to-filter mappings which specify terms, term or range filters, depending on the type of the respective hash value, namely array, range or scalar type like Fixnum, String, etc.

Examples:

CommentIndex.where(id: [1, 2, 3], state: ["approved", "declined"])
CommentIndex.where(id: 1 .. 100)
CommentIndex.where(created_at: Time.parse("2016-01-01") .. Time.parse("2017-01-01"))
CommentIndex.where(id: 1, message: "hello")
CommentIndex.where(state: nil)

Parameters:

  • hash (Hash)

    A field-to-filter mapping specifying filter values for the respective fields

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/search_flip/filterable.rb', line 54

def where(hash)
  hash.inject(fresh) do |memo, (key, value)|
    if value.is_a?(Array)
      memo.filter(terms: { key => value })
    elsif value.is_a?(Range)
      memo.filter(range: { key => { gte: value.min, lte: value.max } })
    elsif value.nil?
      memo.must_not(exists: { field: key })
    else
      memo.filter(term: { key => value })
    end
  end
end