class Elastomer::VersionSupport

VersionSupport holds methods that (a) encapsulate version differences; or (b) give an intention-revealing name to a conditional check.

Attributes

version[R]

Public Class Methods

new(version) click to toggle source

version - an Elasticsearch version string e.g., 2.3.5 or 5.3.0

Raises ArgumentError if version is unsupported.

# File lib/elastomer/version_support.rb, line 11
def initialize(version)
  if version < "2.3" || version >= "5.7"
    raise ArgumentError, "Elasticsearch version #{version} is not supported by elastomer-client"
  end

  @version = version
end

Public Instance Methods

delete_by_query_method() click to toggle source

COMPATIBILITY Return a symbol representing the best supported delete_by_query implementation for this version of Elasticsearch.

# File lib/elastomer/version_support.rb, line 156
def delete_by_query_method
  if es_version_2_x?
    :app_delete_by_query
  else
    :native_delete_by_query
  end
end
es_version_2_x?() click to toggle source

Elasticsearch 2.0 changed some request formats in a non-backward-compatible way. Some tests need to know what version is running to structure requests as expected.

Returns true if Elasticsearch version is 2.x.

# File lib/elastomer/version_support.rb, line 97
def es_version_2_x?
  version >= "2.0.0" && version <  "3.0.0"
end
es_version_5_x?() click to toggle source

Elasticsearch 5.0 changed some request formats in a non-backward-compatible way. Some tests need to know what version is running to structure requests as expected.

Returns true if Elasticsearch version is 5.x.

# File lib/elastomer/version_support.rb, line 106
def es_version_5_x?
  version >= "5.0.0" && version < "6.0.0"
end
fix_op_type!(params = {}) click to toggle source

COMPATIBILITY Internal: VersionSupport maintains dynamically-created lists of acceptable and unacceptable request params by ES version. This just shims that list since those params have leading underscores by default. If we end up with >1 such param, let's make a real thing to handle this.

# File lib/elastomer/version_support.rb, line 147
def fix_op_type!(params = {})
  if es_version_5_x? && params.key?(:op_type)
    params[:op_type] = "op_type"
  end
end
keyword(**args) click to toggle source

COMPATIBILITY: Return a “keyword”-type mapping for a field.

On ES 2.x, this will be a string field with not_analyzed=true. On ES 5+, it will be a keyword field.

# File lib/elastomer/version_support.rb, line 60
def keyword(**args)
  reject_args!(args, :type, :index)

  if es_version_2_x?
    {type: "string", index: "not_analyzed"}.merge(args)
  else
    {type: "keyword"}.merge(args)
  end
end
native_delete_by_query?()

COMPATIBILITY ES 5.X supports `delete_by_query` natively again.

Alias for: es_version_5_x?
op_type(params = {}) click to toggle source

COMPATIBILITY: handle _op_type -> op_type request param conversion for put-if-absent bnehavior Returns the (possibly mutated) params hash supplied by the caller.

www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type

# File lib/elastomer/version_support.rb, line 85
def op_type(params = {})
  if es_version_5_x? && (params.key?(:_op_type) || params.key?("_op_type"))
    params[:op_type] = params.delete(:_op_type)
  end
  params
end
percolator_type() click to toggle source

Wraps version check and param gen where ES version >= 5.x requires percolator type + field defined in mappings

# File lib/elastomer/version_support.rb, line 112
def percolator_type
  if es_version_5_x?
    "percolator"
  else
    ".percolator"
  end
end
query_parse_exception() click to toggle source

COMPATIBILITY ES 2.x reports query parsing exceptions as query_parse_exception whereas ES 5.x reports them as query_shard_exception or parsing_exception depending on when the error occurs.

Returns an Array of Strings to match.

# File lib/elastomer/version_support.rb, line 126
def query_parse_exception
  if es_version_2_x?
    ["query_parsing_exception"]
  else
    ["query_shard_exception", "parsing_exception"]
  end
end
strict_boolean(b) click to toggle source

COMPATIBILITY: return a simple boolean value or legacy {“enabled”: true/false}.

www.elastic.co/guide/en/elasticsearch/reference/5.5/breaking_50_mapping_changes.html#_literal_norms_literal

# File lib/elastomer/version_support.rb, line 73
def strict_boolean(b)
  if es_version_2_x?
    {enabled: b}
  else
    b
  end
end
strict_request_params?()

COMPATIBILITY

ES5 doesn't accept/ignore ambiguous or unexpected req params any more

Alias for: es_version_5_x?
supports_gzip?() click to toggle source

ES 5.X supports GZip-compressed request bodies, but ES 2.4 doesn't?

# File lib/elastomer/version_support.rb, line 165
def supports_gzip?
  es_version_5_x?
end
supports_parent_task_id?() click to toggle source

COMPATIBILITY: Return a boolean indicating if this version supports the `parent_task_id` param in the tasks API - www.elastic.co/guide/en/elasticsearch/reference/5.x/tasks.html

# File lib/elastomer/version_support.rb, line 39
def supports_parent_task_id?
  es_version_5_x?
end
supports_tasks_get?() click to toggle source

COMPATIBILITY: Return a boolean indicating if this version supports the `tasks.get` API - www.elastic.co/guide/en/elasticsearch/reference/5.x/tasks.html

# File lib/elastomer/version_support.rb, line 33
def supports_tasks_get?
  es_version_5_x?
end
supports_warmers?() click to toggle source

COMPATIBILITY: Return a boolean indicating if this version supports warmers. Warmers were removed in ES 5.0.

# File lib/elastomer/version_support.rb, line 21
def supports_warmers?
  es_version_2_x?
end
tasks_new_response_format?() click to toggle source

COMPATIBILITY: The Tasks API is evolving quickly; features, and request/response structure can differ across ES versions

# File lib/elastomer/version_support.rb, line 27
def tasks_new_response_format?
  es_version_5_x?
end
text(**args) click to toggle source

COMPATIBILITY: Return a “text”-type mapping for a field.

On ES 2.x, this will be a string field. On ES 5+, it will be a text field.

# File lib/elastomer/version_support.rb, line 46
def text(**args)
  reject_args!(args, :type, :index)

  if es_version_2_x?
    {type: "string"}.merge(args)
  else
    {type: "text"}.merge(args)
  end
end

Private Instance Methods

reject_args!(args, *names) click to toggle source

Internal: Helper to reject arguments that shouldn't be passed because merging them in would defeat the purpose of a compatibility layer.

# File lib/elastomer/version_support.rb, line 173
def reject_args!(args, *names)
  names.each do |name|
    if args.include?(name.to_s) || args.include?(name.to_sym)
      raise ArgumentError, "Argument '#{name}' is not allowed"
    end
  end
end