class ElasticSearchFramework::Query

Public Class Methods

new(index:) click to toggle source
# File lib/elastic_search_framework/query.rb, line 10
def initialize(index:)
  @index = index
  @parts = []
end

Public Instance Methods

and() click to toggle source
# File lib/elastic_search_framework/query.rb, line 70
def and
  @parts << { type: :and }
  self
end
build() click to toggle source
# File lib/elastic_search_framework/query.rb, line 86
def build
  @expression_string = ''

  @parts.each do |p|
    case p[:type]
      when :field
        @expression_string += ' ' + p[:value].to_s
      when :condition
        @expression_string += p[:expression].to_s + p[:value].to_s
      when :exists
        @expression_string += ' _exists_:' + p[:field].to_s
      when :not_eq
        @expression_string += ' NOT (' + p[:field].to_s + ':' + p[:value].to_s + ')'
      when :and
        @expression_string += ' AND'
      when :or
        @expression_string += ' OR'
      else
        raise 'Invalid query part'
    end
  end

  return @expression_string.strip
end
condition(expression:, value:) click to toggle source
# File lib/elastic_search_framework/query.rb, line 111
def condition(expression:, value:)
  @parts << { type: :condition, expression: expression, value: value }
end
contains?(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 25
def contains?(value)
  condition(expression: ':', value: "*#{value}*")
  self
end
eq(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 20
def eq(value)
  condition(expression: ':', value: value)
  self
end
execute(limit: 10, count: false) click to toggle source
# File lib/elastic_search_framework/query.rb, line 80
def execute(limit: 10, count: false)
  query = build
  repository = ElasticSearchFramework::Repository.new
  repository.query(index: @index, expression: query, limit: limit, count: count)
end
exists?() click to toggle source
# File lib/elastic_search_framework/query.rb, line 60
def exists?
  field = @parts.last
  unless field[:type] == :field
    raise ::InvalidQueryError.new('The exists? query part can only be chained to a field.')
  end
  @parts.pop
  @parts << { type: :exists?, field: field[:value] }
  self
end
gt(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 40
def gt(value)
  condition(expression: ':>', value: value)
  self
end
gt_eq(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 45
def gt_eq(value)
  condition(expression: ':>=', value: value)
  self
end
lt(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 50
def lt(value)
  condition(expression: ':<', value: value)
  self
end
lt_eq(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 55
def lt_eq(value)
  condition(expression: ':<=', value: value)
  self
end
method_missing(name) click to toggle source
# File lib/elastic_search_framework/query.rb, line 15
def method_missing(name)
  @parts << { type: :field, value: name }
  self
end
not_eq(value) click to toggle source
# File lib/elastic_search_framework/query.rb, line 30
def not_eq(value)
  field = @parts.last
  unless field[:type] == :field
    raise ::InvalidQueryError.new('The not_eq query part can only be chained to a field.')
  end
  @parts.pop
  @parts << { type: :not_eq, field: field[:value], value: value }
  self
end
or() click to toggle source
# File lib/elastic_search_framework/query.rb, line 75
def or
  @parts << { type: :or }
  self
end