Module: ElasticGraph::SchemaDefinition::Indexing::DerivedFields::FieldInitializerSupport Private

Defined in:
lib/elastic_graph/schema_definition/indexing/derived_fields/field_initializer_support.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Contains helper logic for field initialization applicable to all types of derived fields.

Constant Summary collapse

EMPTY_PAINLESS_LIST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Painless literal for an empty list, from [the docs](www.elastic.co/guide/en/elasticsearch/painless/8.15/painless-operators-reference.html#list-initialization-operator).

"[]"
EMPTY_PAINLESS_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Painless literal for an empty map, from [the docs](www.elastic.co/guide/en/elasticsearch/painless/8.15/painless-operators-reference.html#map-initialization-operator).

"[:]"

Class Method Summary collapse

Class Method Details

.build_empty_value_initializers(destination_field, leaf_value:) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a list of painless statements that will initialize a given ‘destination_field` path to an empty value.

Returns:

  • (Array<String>)

    a list of painless statements that will initialize a given ‘destination_field` path to an empty value.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elastic_graph/schema_definition/indexing/derived_fields/field_initializer_support.rb', line 27

def self.build_empty_value_initializers(destination_field, leaf_value:)
  snippets = [] # : ::Array[::String]
  path_so_far = [] # : ::Array[::String]

  destination_field.split(".").each do |path_part|
    path_to_this_part = (path_so_far + [path_part]).join(".")
    is_leaf = path_to_this_part == destination_field

    unless is_leaf && leaf_value == :leave_unset
      # The empty value of all parent fields must be an empty painless map, but for a leaf field it can be different.
      empty_value = is_leaf ? leaf_value : EMPTY_PAINLESS_MAP

      snippets << default_source_field_to_empty(path_to_this_part, empty_value.to_s)
      path_so_far << path_part
    end
  end

  snippets
end

.default_source_field_to_empty(field_path, empty_value) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a painless statement that will default a single field to an empty value.

Returns:

  • (String)

    a painless statement that will default a single field to an empty value.



48
49
50
51
52
53
54
# File 'lib/elastic_graph/schema_definition/indexing/derived_fields/field_initializer_support.rb', line 48

def self.default_source_field_to_empty(field_path, empty_value)
  <<~EOS.strip
    if (ctx._source.#{field_path} == null) {
      ctx._source.#{field_path} = #{empty_value};
    }
  EOS
end