Class: JayAPI::Elasticsearch::QueryBuilder::QueryClauses::Range

Inherits:
QueryClause
  • Object
show all
Defined in:
lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb

Overview

Represents a Range query in Elasticsearch More information about this type of query can be found here: www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

Constant Summary collapse

VALID_PARAMS =
%i[gt gte lt lte].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Range

Returns a new instance of Range.

Parameters:

  • params (Hash)

    A hash with parameters for the class.

Options Hash (params):

  • :field (String, Symbol)

    The field where the range query should be applied.

  • :gt (String, Numeric)

    Greater than

  • :gte (String, Numeric)

    Greater than or equal

  • :lt (String, Numeric)

    Less than

  • :lte (String, Numeric)

    Less than or equal

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb', line 24

def initialize(params)
  @field = params.delete(:field) || raise(ArgumentError, "Missing required key 'field'")

  invalid_keys = params.keys - VALID_PARAMS
  raise ArgumentError, "Invalid keys: #{invalid_keys.join(', ')}" if invalid_keys.any?

  params = params.compact
  raise ArgumentError, "At least one of #{VALID_PARAMS.join(', ')} should be given" unless params.any?

  @params = params
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



15
16
17
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb', line 15

def field
  @field
end

#paramsObject (readonly)

Returns the value of attribute params.



15
16
17
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb', line 15

def params
  @params
end

Instance Method Details

#to_hHash

Returns The Hash that represents this query (in Elasticsearch’s format).

Returns:

  • (Hash)

    The Hash that represents this query (in Elasticsearch’s format)



38
39
40
41
42
43
44
# File 'lib/jay_api/elasticsearch/query_builder/query_clauses/range.rb', line 38

def to_h
  {
    range: {
      field => params
    }
  }
end