Module: SearchFlip::PostFilterable
- Included in:
- Criteria
- Defined in:
- lib/search_flip/post_filterable.rb
Overview
The SearchFlip::PostFilterable mixin provides chainable methods like #post_where, #post_exists, #post_range, etc to add and apply search filters after aggregations have already been calculated.
Class Method Summary collapse
Instance Method Summary collapse
-
#post_exists(field) ⇒ SearchFlip::Criteria
Adds a post exists filter to the criteria, which selects all documents for which the specified field has a non-null value.
-
#post_exists_not(field) ⇒ SearchFlip::Criteria
Adds a post exists not filter to the criteria, which selects all documents for which the specified field’s value is null.
-
#post_filter(*args) ⇒ SearchFlip::Criteria
Adds raw post filter queries to the criteria.
-
#post_must(*args) ⇒ SearchFlip::Criteria
Adds raw post must queries to the criteria.
-
#post_must_not(*args) ⇒ SearchFlip::Criteria
Adds raw post must_not queries to the criteria.
-
#post_range(field, options = {}) ⇒ SearchFlip::Criteria
Adds a post 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.
-
#post_search(q, options = {}) ⇒ SearchFlip::Criteria
Adds a post query string query to the criteria while using AND as the default operator unless otherwise specified.
-
#post_should(*args) ⇒ SearchFlip::Criteria
Adds raw post should queries to the criteria.
-
#post_where(hash) ⇒ SearchFlip::Criteria
Adds post 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.
-
#post_where_not(hash) ⇒ SearchFlip::Criteria
Adds post filters to exclude documents in accordance to the supplied hash composed of field-to-filter mappings.
Class Method Details
.included(base) ⇒ Object
24 25 26 27 28 |
# File 'lib/search_flip/post_filterable.rb', line 24 def self.included(base) base.class_eval do attr_accessor :post_search_values, :post_must_values, :post_must_not_values, :post_should_values, :post_filter_values end end |
Instance Method Details
#post_exists(field) ⇒ SearchFlip::Criteria
Adds a post exists filter to the criteria, which selects all documents for which the specified field has a non-null value.
232 233 234 |
# File 'lib/search_flip/post_filterable.rb', line 232 def post_exists(field) post_filter exists: { field: field } end |
#post_exists_not(field) ⇒ SearchFlip::Criteria
Adds a post exists not filter to the criteria, which selects all documents for which the specified field’s value is null.
247 248 249 |
# File 'lib/search_flip/post_filterable.rb', line 247 def post_exists_not(field) post_must_not exists: { field: field } end |
#post_filter(*args) ⇒ SearchFlip::Criteria
Adds raw post filter queries to the criteria.
133 134 135 136 137 |
# File 'lib/search_flip/post_filterable.rb', line 133 def post_filter(*args) fresh.tap do |criteria| criteria.post_filter_values = (post_filter_values || []) + args end end |
#post_must(*args) ⇒ SearchFlip::Criteria
Adds raw post must queries to the criteria.
153 154 155 156 157 |
# File 'lib/search_flip/post_filterable.rb', line 153 def post_must(*args) fresh.tap do |criteria| criteria.post_must_values = (post_must_values || []) + args end end |
#post_must_not(*args) ⇒ SearchFlip::Criteria
Adds raw post must_not queries to the criteria.
173 174 175 176 177 |
# File 'lib/search_flip/post_filterable.rb', line 173 def post_must_not(*args) fresh.tap do |criteria| criteria.post_must_not_values = (post_must_not_values || []) + args end end |
#post_range(field, options = {}) ⇒ SearchFlip::Criteria
Adds a post 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 easily use #post_where, etc. Check out the ElasticSearch docs for further details regarding the range filter.
217 218 219 |
# File 'lib/search_flip/post_filterable.rb', line 217 def post_range(field, = {}) post_filter range: { field => } end |
#post_search(q, options = {}) ⇒ SearchFlip::Criteria
Adds a post query string query to the criteria while using AND as the default operator unless otherwise specified. Check out the ElasticSearch docs for further details.
44 45 46 47 48 49 50 |
# File 'lib/search_flip/post_filterable.rb', line 44 def post_search(q, = {}) raise(SearchFlip::NotSupportedError) if SearchFlip.version.to_i < 2 fresh.tap do |criteria| criteria.post_search_values = (post_search_values || []) + [query_string: { query: q, :default_operator => :AND }.merge()] if q.to_s.strip.length > 0 end end |
#post_should(*args) ⇒ SearchFlip::Criteria
Adds raw post should queries to the criteria.
193 194 195 196 197 |
# File 'lib/search_flip/post_filterable.rb', line 193 def post_should(*args) fresh.tap do |criteria| criteria.post_should_values = (post_should_values || []) + args end end |
#post_where(hash) ⇒ SearchFlip::Criteria
Adds post 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.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/search_flip/post_filterable.rb', line 74 def post_where(hash) hash.inject(fresh) do |memo, (key, value)| if value.is_a?(Array) memo.post_filter terms: { key => value } elsif value.is_a?(Range) memo.post_filter range: { key => { gte: value.min, lte: value.max } } else memo.post_filter term: { key => value } end end end |
#post_where_not(hash) ⇒ SearchFlip::Criteria
Adds post filters to exclude documents in accordance to the supplied hash composed of field-to-filter mappings. Check out #post_where for further details.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/search_flip/post_filterable.rb', line 107 def post_where_not(hash) hash.inject(fresh) do |memo, (key,value)| if value.is_a?(Array) memo.post_must_not terms: { key => value } elsif value.is_a?(Range) memo.post_must_not range: { key => { gte: value.min, lte: value.max } } else memo.post_must_not term: { key => value } end end end |