Class: Elasticfusion::Search::Query::Visitors::Elasticsearch

Inherits:
PolyadicTree show all
Defined in:
lib/elasticfusion/search/query/visitors/elasticsearch.rb

Constant Summary collapse

OPERATORS =
{ and: :must,
or: :should }.freeze

Instance Method Summary collapse

Methods inherited from PolyadicTree

#accept, #rewrite

Methods inherited from Elasticfusion::Search::Query::Visitor

#accept, #visit, visitor_method

Constructor Details

#initialize(keyword_field, mapping) ⇒ Elasticsearch

Returns a new instance of Elasticsearch.



10
11
12
13
# File 'lib/elasticfusion/search/query/visitors/elasticsearch.rb', line 10

def initialize(keyword_field, mapping)
  @keyword_field = keyword_field
  @sanitizer = ValueSanitizer.new(mapping)
end

Instance Method Details

#visit_FieldTerm(node) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/elasticfusion/search/query/visitors/elasticsearch.rb', line 35

def visit_FieldTerm(node)
  value = @sanitizer.value(node.value, field: node.field)

  if node.qualifier
    { range: { node.field.to_sym => { node.qualifier => value } } }
  else
    { term: { node.field.to_sym => value } }
  end
end

#visit_NegatedClause(node) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/elasticfusion/search/query/visitors/elasticsearch.rb', line 25

def visit_NegatedClause(node)
  clause = if node.body.respond_to?(:op) && node.body.op == :and
    node.body.children.map { |n| visit(n) }
  else
    [visit(node.body)]
  end

  { bool: { must_not: clause } }
end

#visit_PolyadicExpression(node) ⇒ Object



18
19
20
21
22
23
# File 'lib/elasticfusion/search/query/visitors/elasticsearch.rb', line 18

def visit_PolyadicExpression(node)
  operator = OPERATORS[node.op]
  operands = node.children.map { |n| visit(n) }

  { bool: { operator => operands } }
end

#visit_Term(node) ⇒ Object



45
46
47
# File 'lib/elasticfusion/search/query/visitors/elasticsearch.rb', line 45

def visit_Term(node)
  { term: { @keyword_field => node.body } }
end