Class: Stretchy::Builders::QueryBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stretchy/builders/query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Returns a new instance of QueryBuilder.



11
12
13
14
# File 'lib/stretchy/builders/query_builder.rb', line 11

def initialize
  @matches    = Hash.new { [] }
  @query_opts = Hash.new { {} }
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



9
10
11
# File 'lib/stretchy/builders/query_builder.rb', line 9

def matches
  @matches
end

#query_optsObject (readonly)

Returns the value of attribute query_opts.



9
10
11
# File 'lib/stretchy/builders/query_builder.rb', line 9

def query_opts
  @query_opts
end

Instance Method Details

#add_matches(field, new_matches, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/stretchy/builders/query_builder.rb', line 16

def add_matches(field, new_matches, options = {})
  @matches[field] += Array(new_matches)
  opts = {}
  [:operator, :slop, :minimum_should_match, :type].each do |opt|
    opts[opt] = options[opt] if options[opt]
  end
  @query_opts[field] = opts
end

#to_queriesObject



25
26
27
28
29
30
31
32
33
# File 'lib/stretchy/builders/query_builder.rb', line 25

def to_queries
  matches.map do |field, phrases|
    opts = query_opts[field].merge(
      field: field,
      string: phrases.flatten.join(' ')
    )
    Queries::MatchQuery.new(opts)
  end
end