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
# File 'lib/waistband/query.rb', line 11

def initialize(index, search_term, options = {})
  @index          = index
  @search_term    = search_term
  @wildcards      = []
  @fields         = []
  @ranges         = []
  @sorts          = []
  @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_fields(*fields) ⇒ Object Also known as: add_field



47
48
49
50
# File 'lib/waistband/query.rb', line 47

def add_fields(*fields)
  @fields |= fields
  @fields = @fields.compact.uniq
end

#add_match(field) ⇒ Object



43
44
45
# File 'lib/waistband/query.rb', line 43

def add_match(field)
  @match = field
end

#add_optional_terms(key, words) ⇒ Object Also known as: add_optional_term



76
77
78
79
80
81
# File 'lib/waistband/query.rb', line 76

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



60
61
62
63
64
65
66
# File 'lib/waistband/query.rb', line 60

def add_range(field, min, max)
  @ranges << {
    field: field,
    min: min,
    max: max
  }
end

#add_sort(key, ord) ⇒ Object



84
85
86
87
88
89
# File 'lib/waistband/query.rb', line 84

def add_sort(key, ord)
  @sorts << {
    key: key,
    ord: ord
  }
end

#add_terms(key, words) ⇒ Object Also known as: add_term



68
69
70
71
72
73
# File 'lib/waistband/query.rb', line 68

def add_terms(key, words)
  @terms[key] ||= {
    keywords: []
  }
  @terms[key][:keywords] += prep_words_uniquely(words)
end

#add_wildcard(wildcard, value) ⇒ Object



53
54
55
56
57
58
# File 'lib/waistband/query.rb', line 53

def add_wildcard(wildcard, value)
  @wildcards << {
    wildcard: wildcard,
    value: value
  }
end

#hitsObject



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

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

#paginated_resultsObject



24
25
26
27
# File 'lib/waistband/query.rb', line 24

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



29
30
31
32
33
# File 'lib/waistband/query.rb', line 29

def results
  hits.map do |hit|
    Waistband::QueryResult.new(hit)
  end
end

#total_resultsObject



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

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