Class: Waistband::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/waistband/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @index          = index
  @search_term    = search_term
  @wildcards      = []
  @fields         = []
  @ranges         = []
  @sorts          = []
  @terms          = {}
  @exclude_terms  = {}
  @optional_terms = {}
  @page           = (options[:page] || 1).to_i
  @page_size      = (options[:page_size] || 20).to_i
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



9
10
11
# File 'lib/waistband/query.rb', line 9

def page
  @page
end

#page_sizeObject

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

#hitsObject



36
37
38
# File 'lib/waistband/query.rb', line 36

def hits
  execute!['hits']['hits'] rescue []
end

#paginated_resultsObject



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

#resultsObject



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_resultsObject



40
41
42
# File 'lib/waistband/query.rb', line 40

def total_results
  execute!['hits']['total'] rescue 0
end