Class: Waistband::Query
- Inherits:
-
Object
- Object
- Waistband::Query
- Defined in:
- lib/waistband/query.rb
Instance Attribute Summary collapse
-
#page ⇒ Object
Returns the value of attribute page.
-
#page_size ⇒ Object
Returns the value of attribute page_size.
Instance Method Summary collapse
- #add_exclude_terms(key, words) ⇒ Object (also: #add_exclude_term)
- #add_fields(*fields) ⇒ Object (also: #add_field)
- #add_match(field) ⇒ Object
- #add_optional_terms(key, words) ⇒ Object (also: #add_optional_term)
- #add_range(field, min, max) ⇒ Object
- #add_sort(key, ord) ⇒ Object
- #add_terms(key, words) ⇒ Object (also: #add_term)
- #add_wildcard(wildcard, value) ⇒ Object
- #hits ⇒ Object
-
#initialize(index, search_term, options = {}) ⇒ Query
constructor
A new instance of Query.
- #paginated_results ⇒ Object
- #results ⇒ Object
- #total_results ⇒ Object
Constructor Details
#initialize(index, search_term, options = {}) ⇒ Query
Returns a new instance of Query.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/waistband/query.rb', line 11 def initialize(index, search_term, = {}) @index = index @search_term = search_term @wildcards = [] @fields = [] @ranges = [] @sorts = [] @terms = {} @exclude_terms = {} @optional_terms = {} @page = ([:page] || 1).to_i @page_size = ([:page_size] || 20).to_i end |
Instance Attribute Details
#page ⇒ Object
Returns the value of attribute page.
9 10 11 |
# File 'lib/waistband/query.rb', line 9 def page @page end |
#page_size ⇒ Object
Returns the value of attribute page_size.
9 10 11 |
# File 'lib/waistband/query.rb', line 9 def page_size @page_size end |
Instance Method Details
#add_exclude_terms(key, words) ⇒ Object Also known as: add_exclude_term
77 78 79 80 81 82 |
# File 'lib/waistband/query.rb', line 77 def add_exclude_terms(key, words) @exclude_terms[key] ||= { keywords: [] } @exclude_terms[key][:keywords] += prep_words_uniquely(words) end |
#add_fields(*fields) ⇒ Object Also known as: add_field
48 49 50 51 |
# File 'lib/waistband/query.rb', line 48 def add_fields(*fields) @fields |= fields @fields = @fields.compact.uniq end |
#add_match(field) ⇒ Object
44 45 46 |
# File 'lib/waistband/query.rb', line 44 def add_match(field) @match = field end |
#add_optional_terms(key, words) ⇒ Object Also known as: add_optional_term
85 86 87 88 89 90 |
# File 'lib/waistband/query.rb', line 85 def add_optional_terms(key, words) @optional_terms[key] ||= { keywords: [] } @optional_terms[key][:keywords] += prep_words_uniquely(words) end |
#add_range(field, min, max) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/waistband/query.rb', line 61 def add_range(field, min, max) @ranges << { field: field, min: min, max: max } end |
#add_sort(key, ord) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/waistband/query.rb', line 93 def add_sort(key, ord) @sorts << { key: key, ord: ord } end |
#add_terms(key, words) ⇒ Object Also known as: add_term
69 70 71 72 73 74 |
# File 'lib/waistband/query.rb', line 69 def add_terms(key, words) @terms[key] ||= { keywords: [] } @terms[key][:keywords] += prep_words_uniquely(words) end |
#add_wildcard(wildcard, value) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/waistband/query.rb', line 54 def add_wildcard(wildcard, value) @wildcards << { wildcard: wildcard, value: value } end |
#hits ⇒ Object
36 37 38 |
# File 'lib/waistband/query.rb', line 36 def hits execute!['hits']['hits'] rescue [] end |
#paginated_results ⇒ Object
25 26 27 28 |
# File 'lib/waistband/query.rb', line 25 def paginated_results return Kaminari.paginate_array(results, total_count: total_results).page(@page).per(@page_size) if defined?(Kaminari) raise "Please include the `kaminari` gem to use this method!" end |
#results ⇒ Object
30 31 32 33 34 |
# File 'lib/waistband/query.rb', line 30 def results hits.map do |hit| Waistband::QueryResult.new(hit) end end |
#total_results ⇒ Object
40 41 42 |
# File 'lib/waistband/query.rb', line 40 def total_results execute!['hits']['total'] rescue 0 end |