Module: SearchFlip::Filterable
- Included in:
- Aggregation, Criteria
- Defined in:
- lib/search_flip/filterable.rb
Overview
The SearchFlip::Filterable mixin provides chainable methods like #where, #exists, #range, etc to add search filters to a criteria.
Class Method Summary collapse
Instance Method Summary collapse
-
#exists(field) ⇒ SearchFlip::Criteria
Adds an exists filter to the criteria, which selects all documents for which the specified field has a non-null value.
-
#exists_not(field) ⇒ SearchFlip::Criteria
Adds an exists not filter to the criteria, which selects all documents for which the specified field’s value is null.
-
#filter(*args) ⇒ SearchFlip::Criteria
Adds raw filter queries to the criteria.
-
#match_all(options = {}) ⇒ SearchFlip::Criteria
Adds a match all filter/query to the criteria, which simply matches all documents.
-
#must(*args) ⇒ SearchFlip::Criteria
Adds raw must queries to the criteria.
-
#must_not(*args) ⇒ SearchFlip::Criteria
Adds raw must_not queries to the criteria.
-
#range(field, options = {}) ⇒ SearchFlip::Criteria
Adds a range filter to the criteria without being forced to specify the left and right end of the range, such that you can eg simply specify lt, lte, gt and gte.
-
#search(q, options = {}) ⇒ SearchFlip::Criteria
Adds a query string query to the criteria while using AND as the default operator unless otherwise specified.
-
#should(*args) ⇒ SearchFlip::Criteria
Adds raw should queries to the criteria.
-
#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.
-
#where_not(hash) ⇒ SearchFlip::Criteria
Adds filters to exclude documents in accordance to the supplied hash composed of field-to-filter mappings.
Class Method Details
.included(base) ⇒ Object
12 13 14 15 16 |
# File 'lib/search_flip/filterable.rb', line 12 def self.included(base) base.class_eval do attr_accessor :search_values, :must_values, :must_not_values, :should_values, :filter_values end end |
Instance Method Details
#exists(field) ⇒ SearchFlip::Criteria
Adds an exists filter to the criteria, which selects all documents for which the specified field has a non-null value.
221 222 223 |
# File 'lib/search_flip/filterable.rb', line 221 def exists(field) filter exists: { field: field } end |
#exists_not(field) ⇒ SearchFlip::Criteria
Adds an exists not filter to the criteria, which selects all documents for which the specified field’s value is null.
235 236 237 |
# File 'lib/search_flip/filterable.rb', line 235 def exists_not(field) must_not exists: { field: field } end |
#filter(*args) ⇒ SearchFlip::Criteria
Adds raw filter queries to the criteria.
110 111 112 113 114 |
# File 'lib/search_flip/filterable.rb', line 110 def filter(*args) fresh.tap do |criteria| criteria.filter_values = (filter_values || []) + args end end |
#match_all(options = {}) ⇒ SearchFlip::Criteria
Adds a match all filter/query to the criteria, which simply matches all documents. This can be eg be used within filter aggregations or for filter chaining. Check out the ElasticSearch docs for further details.
207 208 209 |
# File 'lib/search_flip/filterable.rb', line 207 def match_all( = {}) filter match_all: end |
#must(*args) ⇒ SearchFlip::Criteria
Adds raw must queries to the criteria.
126 127 128 129 130 |
# File 'lib/search_flip/filterable.rb', line 126 def must(*args) fresh.tap do |criteria| criteria.must_values = (must_values || []) + args end end |
#must_not(*args) ⇒ SearchFlip::Criteria
Adds raw must_not queries to the criteria.
142 143 144 145 146 |
# File 'lib/search_flip/filterable.rb', line 142 def must_not(*args) fresh.tap do |criteria| criteria.must_not_values = (must_not_values || []) + args end end |
#range(field, options = {}) ⇒ SearchFlip::Criteria
Adds a range filter to the criteria without being forced to specify the left and right end of the range, such that you can eg simply specify lt, lte, gt and gte. For fully specified ranges, you can as well use #where, etc. Check out the ElasticSearch docs for further details regarding the range filter.
179 180 181 |
# File 'lib/search_flip/filterable.rb', line 179 def range(field, = {}) filter range: { field => } end |
#search(q, options = {}) ⇒ SearchFlip::Criteria
Adds a query string query to the criteria while using AND as the default operator unless otherwise specified. Check out the ElasticSearch docs for further details.
32 33 34 35 36 |
# File 'lib/search_flip/filterable.rb', line 32 def search(q, = {}) fresh.tap do |criteria| criteria.search_values = (search_values || []) + [query_string: { query: q, :default_operator => :AND }.merge()] if q.to_s.strip.length > 0 end end |
#should(*args) ⇒ SearchFlip::Criteria
Adds raw should queries to the criteria.
158 159 160 161 162 |
# File 'lib/search_flip/filterable.rb', line 158 def should(*args) fresh.tap do |criteria| criteria.should_values = (should_values || []) + args end end |
#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.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/search_flip/filterable.rb', line 55 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.exists_not key else memo.filter term: { key => value } end end end |
#where_not(hash) ⇒ SearchFlip::Criteria
Adds filters to exclude documents in accordance to the supplied hash composed of field-to-filter mappings. Check out #where for further details.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/search_flip/filterable.rb', line 86 def where_not(hash) hash.inject(fresh) do |memo, (key, value)| if value.is_a?(Array) memo.must_not terms: { key => value } elsif value.is_a?(Range) memo.must_not range: { key => { gte: value.min, lte: value.max } } elsif value.nil? memo.exists key else memo.must_not term: { key => value } end end end |