Class: Caoutsearch::Filter::Match

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

Constant Summary collapse

QUERY_STRING_REGEXP =
%r{
  (?:[*?][^\s*?]|[^\s*?][*?]|(?:^|\s)(?:AND|OR|NOT)(?:$|\s)|^\*$)
}x
STRIPPED_OPERATOR_REGEXP =
%r{
  (?:^\s*(?:AND|OR)\*$|^\s*(?:AND|OR)\s+|\s+(?:AND|OR)\s*$)
}x

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
# File 'lib/caoutsearch/filter/match.rb', line 6

def filter
  if use_query_string?
    {
      simple_query_string: {
        fields: [key],
        query: sanitized_for_query_string,
        default_operator: "and",
        analyze_wildcard: true
      }
    }
  elsif multiple_words?
    {match: {key => {query: value, operator: "and"}}}
  else
    {match: {key => value}}
  end
end

#multiple_words?Boolean

Returns:



27
28
29
# File 'lib/caoutsearch/filter/match.rb', line 27

def multiple_words?
  value.is_a?(String) && value.squish.include?(" ")
end

#nested_query?Boolean

Returns:



23
24
25
# File 'lib/caoutsearch/filter/match.rb', line 23

def nested_query?
  nested_path? && (multiple_words? || !include_in_parent?)
end

#sanitized_for_query_stringObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/caoutsearch/filter/match.rb', line 45

def sanitized_for_query_string
  # Do not allow setting fields in query string
  clean_value = Caoutsearch::Search::Sanitizer.sanitize(value, ":")

  # Delete leading and trailing operators
  # Example : " OR ANGE"  => "ANGE"
  # Example : "MASSE OR " => "MASSE"

  clean_value.gsub(STRIPPED_OPERATOR_REGEXP, "")
end

#use_query_string?Boolean

Returns:



41
42
43
# File 'lib/caoutsearch/filter/match.rb', line 41

def use_query_string?
  QUERY_STRING_REGEXP.match?(value)
end