Class: ElasticGraph::SchemaDefinition::Indexing::Field Private

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/schema_definition/indexing/field.rb

Overview

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

Represents a field in a JSON document during indexing.

Constant Summary collapse

JSON_SCHEMA_OVERRIDES_BY_MAPPING_TYPE =

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.

Note:

We don’t handle ‘integer` here because it’s the default numeric type (handled by our definition of the ‘Int` scalar type).

Note:

Likewise, we don’t handle ‘long` here because a custom scalar type must be used for that since GraphQL’s ‘Int` type can’t handle long values.

JSON schema overrides that automatically apply to specific mapping types so that the JSON schema validation will reject values which cannot be indexed into fields of a specific mapping type.

{
  "byte" => {"minimum" => -(2**7), "maximum" => (2**7) - 1},
  "short" => {"minimum" => -(2**15), "maximum" => (2**15) - 1},
  "keyword" => {"maxLength" => DEFAULT_MAX_KEYWORD_LENGTH},
  "text" => {"maxLength" => DEFAULT_MAX_TEXT_LENGTH}
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalized_mapping_hash_for(fields) ⇒ Hash<String, Object>

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.

Builds a hash containing the mapping for the provided fields, normalizing it in the same way that the datastore does so that consistency checks between our index configuration and what’s in the datastore work properly.

Parameters:

  • fields (Array<Field>)

    fields to generate a mapping hash from

Returns:

  • (Hash<String, Object>)

    generated mapping hash



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/elastic_graph/schema_definition/indexing/field.rb', line 87

def self.normalized_mapping_hash_for(fields)
  # When an object field has `properties`, the datastore normalizes the mapping by dropping
  # the `type => object` (it's implicit, as `properties` are only valid on an object...).
  # OTOH, when there are no properties, the datastore normalizes the mapping by dropping the
  # empty `properties` entry and instead returning `type => object`.
  return {"type" => "object"} if fields.empty?

  # Partition the fields into runtime fields and normal fields based on the presence of runtime_script
  runtime_fields, normal_fields = fields.partition(&:runtime_field_script)

  mapping_hash = {
    "properties" => normal_fields.to_h { |f| [f.name_in_index, f.mapping] }
  }
  unless runtime_fields.empty?
    mapping_hash["runtime"] = runtime_fields.to_h do |f|
      [f.name_in_index, f.mapping.merge({"script" => {"source" => f.runtime_field_script}})]
    end
  end

  mapping_hash
end

Instance Method Details

#json_schemaHash<String, Object>

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 the JSON schema definition for this field. The returned object should be composed entirely of Ruby primitives that, when converted to a JSON string, match the requirements of [the JSON schema spec](json-schema.org/).

Returns:

  • (Hash<String, Object>)

    the JSON schema definition for this field. The returned object should be composed entirely of Ruby primitives that, when converted to a JSON string, match the requirements of [the JSON schema spec](json-schema.org/).



68
69
70
71
72
73
74
# File 'lib/elastic_graph/schema_definition/indexing/field.rb', line 68

def json_schema
  json_schema_layers
    .reverse # resolve layers from innermost to outermost wrappings
    .reduce(inner_json_schema) { |acc, layer| process_layer(layer, acc) }
    .merge(outer_json_schema_customizations)
    .then { |h| Support::HashUtil.stringify_keys(h) }
end

#json_schema_metadataJSONSchemaFieldMetadata

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 additional ElasticGraph metadata to be stored in the JSON schema for this field.

Returns:



77
78
79
# File 'lib/elastic_graph/schema_definition/indexing/field.rb', line 77

def 
  JSONSchemaFieldMetadata.new(type: type.name, name_in_index: name_in_index)
end

#mappingHash<String, Object>

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 the mapping for this field. The returned hash should be composed entirely of Ruby primitives that, when converted to a JSON string, match the structure required by [Elasticsearch](www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html).

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/elastic_graph/schema_definition/indexing/field.rb', line 49

def mapping
  @mapping ||= begin
    raw_mapping = indexing_field_type
      .to_mapping
      .merge(Support::HashUtil.stringify_keys(mapping_customizations))

    if (object_type = type.fully_unwrapped.as_object_type) && type.list? && mapping_customizations[:type] == "nested"
      # If it's an object list field using the `nested` type, we need to add a `__counts` field to
      # the mapping for all of its subfields which are lists.
      ListCountsMapping.merged_into(raw_mapping, for_type: object_type)
    else
      raw_mapping
    end
  end
end