Class: Elasticfusion::Search::Query::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticfusion/search/query/parser.rb

Constant Summary collapse

FIELD_QUALIFIERS =
{ 'less than' => :lt,
'more than' => :gt,
'earlier than' => :lt,
'later than' => :gt }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(query, searchable_fields = []) ⇒ Parser

Returns a new instance of Parser.



9
10
11
# File 'lib/elasticfusion/search/query/parser.rb', line 9

def initialize(query, searchable_fields = [])
  @lexer = Lexer.new(query, searchable_fields)
end

Instance Method Details

#astObject

query = disjunction

;

disjunction = conjunction , [ ( “OR” | “|” ) , disjunction ]

;

conjunction = boolean clause , [ ( “AND” | “,” ) , conjunction ]

;

boolean clause = ( “NOT” | “-” ) , boolean clause

| clause
;

clause = parenthesized expression

| field term
| term
;

parenthesized expression = “(” , disjunction , “)”

;

field term = field , “:” , [ field qualifier ] , safe string

;

term = quoted string

| string with balanced parentheses
;


38
39
40
# File 'lib/elasticfusion/search/query/parser.rb', line 38

def ast
  disjunction
end

#boolean_clauseObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elasticfusion/search/query/parser.rb', line 76

def boolean_clause
  negation = match :not
  skip :whitespace

  if negation
    body = boolean_clause
    redundant_negation = body.is_a?(NegatedClause)

    if redundant_negation
      body.body
    else
      NegatedClause.new body
    end
  else
    clause
  end
end

#clauseObject



94
95
96
# File 'lib/elasticfusion/search/query/parser.rb', line 94

def clause
  parenthesized_expression || field_term || term
end

#conjunctionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/elasticfusion/search/query/parser.rb', line 59

def conjunction
  skip :whitespace
  left = boolean_clause

  skip :whitespace
  connective = match :and

  skip :whitespace
  right = conjunction if connective

  if right
    Expression.new :and, left, right
  else
    left
  end
end

#disjunctionObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/elasticfusion/search/query/parser.rb', line 42

def disjunction
  skip :whitespace
  left = conjunction

  skip :whitespace
  connective = match :or

  skip :whitespace
  right = disjunction if connective

  if right
    Expression.new :or, left, right
  else
    left
  end
end

#field_qualifierObject



138
139
140
141
142
143
# File 'lib/elasticfusion/search/query/parser.rb', line 138

def field_qualifier
  skip :whitespace

  qualifier = match :field_qualifier
  FIELD_QUALIFIERS[qualifier]
end

#field_termObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/elasticfusion/search/query/parser.rb', line 113

def field_term
  field = match_field

  if field
    qualifier = field_qualifier

    skip :whitespace if qualifier

    field_query = safe_string

    FieldTerm.new field, qualifier, field_query
  end
end

#parenthesized_expressionObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/elasticfusion/search/query/parser.rb', line 98

def parenthesized_expression
  opening_parens = left_parentheses

  if opening_parens
    body = disjunction
    closing_parens = right_parentheses(opening_parens)

    if opening_parens == closing_parens
      body
    else
      raise ImbalancedParenthesesError
    end
  end
end

#termObject



127
128
129
130
131
# File 'lib/elasticfusion/search/query/parser.rb', line 127

def term
  string = quoted_string || string_with_balanced_parentheses

  Term.new string.downcase
end