Class: Waistband::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryHelpers

#hits, #paginated_results, #results, #total_results

Constructor Details

#initialize(index, search_term, options = {}) ⇒ Query

Returns a new instance of Query.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/waistband/query.rb', line 13

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.



11
12
13
# File 'lib/waistband/query.rb', line 11

def page
  @page
end

#page_sizeObject

Returns the value of attribute page_size.



11
12
13
# File 'lib/waistband/query.rb', line 11

def page_size
  @page_size
end

Instance Method Details

#add_exclude_terms(key, words) ⇒ Object Also known as: add_exclude_term



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

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



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

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

#add_match(field) ⇒ Object



27
28
29
# File 'lib/waistband/query.rb', line 27

def add_match(field)
  @match = field
end

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



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

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

#add_random_sortObject



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

def add_random_sort
  @random_sort = {
    _script:  {
      script:   "Math.random()",
      type:     :number,
      params:   {},
      order:    :asc
    }
  }
end

#add_range(field, min, max) ⇒ Object



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

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

#add_sort(key, ord) ⇒ Object



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

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

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



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

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

#add_wildcard(wildcard, value) ⇒ Object



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

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