Module: ElasticGraph::SchemaDefinition::Mixins::HasTypeInfo

Included in:
SchemaElements::Field, SchemaElements::ScalarType, SchemaElements::TypeWithSubfields
Defined in:
elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/has_type_info.rb

Overview

Mixin used to specify non-GraphQL type info on schema elements. Exists as a mixin so we can apply the same consistent API to every place we need to use this. Currently it's used in 3 places:

Constant Summary collapse

CUSTOMIZABLE_DATASTORE_PARAMS =

Set of mapping parameters that it makes sense to allow customization of, based on the Elasticsearch docs.

Set[
  :analyzer,
  :eager_global_ordinals,
  :enabled,
  :fields,
  :format,
  :index,
  :meta, # not actually in the doc above. Added to support some `index_configurator` tests on 7.9+.
  :norms,
  :null_value,
  :search_analyzer,
  :type
]

Instance Method Summary collapse

Instance Method Details

#mapping(**options) ⇒ void

This method returns an undefined value.

Defines the Elasticsearch/OpenSearch field mapping type and mapping parameters for a field or type. The options passed here will be included in the generated datastore_config.yaml artifact that ElasticGraph uses to configure Elasticsearch/OpenSearch.

Can be called multiple times; each time, the options will be merged into the existing options.

This is required on a SchemaElements::ScalarType; without it, ElasticGraph would have no way to know how the datatype should be indexed in the datastore.

On a SchemaElements::Field, this can be used to customize how a field is indexed. For example, String fields are normally indexed as keywords; to instead index a String field for full text search, you’d need to configure mapping type: "text".

On a SchemaElements::ObjectType, this can be used to use a specific Elasticsearch/OpenSearch data type for something that is modeled as an object in GraphQL. For example, we use it for the GeoLocation type so they get indexed in Elasticsearch using the geo_point type.

Examples:

Define the mapping of a custom scalar type

ElasticGraph.define_schema do |schema|
  schema.scalar_type "URL" do |t|
    t.mapping type: "keyword"
  end
end

Customize the mapping of a field

ElasticGraph.define_schema do |schema|
  schema.object_type "Card" do |t|
    t.field "id", "ID!"

    t.field "cardholderName", "String" do |f|
      # index this field for full text search
      f.mapping type: "text"
    end

    t.field "expYear", "Int" do |f|
      # Use a smaller numeric type to save space in the datastore
      f.mapping type: "short"
    end

    t.index "cards"
  end
end

Parameters:



87
88
89
90
91
92
93
94
95
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/has_type_info.rb', line 87

def mapping(**options)
  param_diff = (options.keys.to_set - CUSTOMIZABLE_DATASTORE_PARAMS).to_a

  unless param_diff.empty?
    raise Errors::SchemaError, "Some configured mapping overrides are unsupported: #{param_diff.inspect}"
  end

  mapping_options.update(options)
end

#mapping_optionsHash<Symbol, Object>

Returns datastore mapping options.

Returns:

  • (Hash<Symbol, Object>)

    datastore mapping options



21
22
23
# File 'elasticgraph-schema_definition/lib/elastic_graph/schema_definition/mixins/has_type_info.rb', line 21

def mapping_options
  @mapping_options ||= {}
end