Class: Caoutsearch::Filter::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/caoutsearch/filter/default.rb

Instance Attribute Summary

Attributes inherited from Base

#key, #options, #original_value, #type

Instance Method Summary collapse

Methods inherited from Base

#as_json, call, #initialize, #value

Constructor Details

This class inherits a constructor from Caoutsearch::Filter::Base

Instance Method Details

#filterObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/caoutsearch/filter/default.rb', line 6

def filter
  if value.nil? && %w[text keyword].include?(type)
    {
      bool: {
        should: [
          {bool: {must_not: {exists: {field: key}}}},
          {term: {key => ""}}
        ]
      }
    }

  elsif value.nil?
    {bool: {must_not: {exists: {field: key}}}}

  elsif value.is_a?(Array) && value.any?(&:nil?)
    terms = []
    terms << {bool: {must_not: {exists: {field: key}}}}

    terms_values = value.compact
    terms_values += [""] if %w[text keyword].include?(type)

    if terms_values.size == 1
      terms << {term: {key => terms_values[0]}}
    elsif terms_values.size > 1
      terms << {terms: {key => terms_values}}
    end

    if terms.size == 1
      terms[0]
    else
      {bool: {should: terms}}
    end

  elsif value.is_a?(Array) && value.size == 1
    {term: {key => value[0]}}

  elsif value.is_a?(Array)
    {terms: {key => value}}

  else
    {term: {key => value}}
  end
end