Module: Caoutsearch::Search::Query::Boolean

Included in:
Base
Defined in:
lib/caoutsearch/search/query/boolean.rb

Instance Method Summary collapse

Instance Method Details

#flatten_bool_terms(operator, raw_terms) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/caoutsearch/search/query/boolean.rb', line 47

def flatten_bool_terms(operator, raw_terms)
  terms = []

  raw_terms.flatten.each do |value|
    if value.is_a?(Hash) && value.keys == [:bool] && value[:bool].keys == [operator]
      terms += Array.wrap(value.dig(:bool, operator))
    else
      terms << value
    end
  end

  terms.uniq
end

#must_filter_on(terms) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/caoutsearch/search/query/boolean.rb', line 21

def must_filter_on(terms)
  terms = flatten_bool_terms(:must, terms)
  return if terms.empty?

  if terms.include?(Caoutsearch::Filter::NONE)
    filters << Caoutsearch::Filter::NONE
  elsif terms.size == 1
    filters << terms[0]
  elsif terms.size > 1
    filters.push(*terms)
  end
end

#must_not_filter_on(terms) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/caoutsearch/search/query/boolean.rb', line 34

def must_not_filter_on(terms)
  terms = flatten_bool_terms(:must_not, terms)
  terms = terms.without(Caoutsearch::Filter::NONE)
  return if terms.empty?

  filters <<
    if terms.size == 1
      {bool: {must_not: terms[0]}}
    else
      {bool: {must_not: terms}}
    end
end

#should_filter_on(terms) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/caoutsearch/search/query/boolean.rb', line 7

def should_filter_on(terms)
  terms = flatten_bool_terms(:should, terms)
  terms_without_none = terms.without(Caoutsearch::Filter::NONE)
  return if terms.empty?

  if terms.size == 1
    filters << terms[0]
  elsif terms_without_none.size == 1
    filters << terms_without_none[0]
  elsif terms_without_none.size > 1
    filters << {bool: {should: terms_without_none}}
  end
end