Module: ElasticGraph::SchemaDefinition::Mixins::HasIndices

Included in:
SchemaElements::InterfaceType, SchemaElements::ObjectType, SchemaElements::UnionType
Defined in:
lib/elastic_graph/schema_definition/mixins/has_indices.rb

Overview

Provides APIs for defining datastore indices.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runtime_metadata_overridesObject



19
20
21
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 19

def 
  @runtime_metadata_overrides
end

Instance Method Details

#abstract?Boolean

Abstract types are rare, so return false. This can be overridden in the host class.

Returns:

  • (Boolean)


84
85
86
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 84

def abstract?
  false
end

#derive_indexed_type_fields(name, from_id:, route_with: nil, rollover_with: nil) {|Indexing::DerivedIndexedType| ... } ⇒ void

This method returns an undefined value.

Configures the ElasticGraph indexer to derive another type from this indexed type, using the ‘from_id` field as the source of the `id` of the derived type, and the provided block for the definitions of the derived fields.

Examples:

Derive a ‘Course` type from `StudentCourseEnrollment` events

ElasticGraph.define_schema do |schema|
  # `StudentCourseEnrollment` is a directly indexed type.
  schema.object_type "StudentCourseEnrollment" do |t|
    t.field "id", "ID"
    t.field "courseId", "ID"
    t.field "courseName", "String"
    t.field "studentName", "String"
    t.field "courseStartDate", "Date"

    t.index "student_course_enrollments"

    # Here we define how the `Course` indexed type  is derived when we index `StudentCourseEnrollment` events.
    t.derive_indexed_type_fields "Course", from_id: "courseId" do |derive|
      # `derive` is an instance of `DerivedIndexedType`.
      derive.immutable_value "name", from: "courseName"
      derive.append_only_set "students", from: "studentName"
      derive.min_value "firstOfferedDate", from: "courseStartDate"
      derive.max_value "mostRecentlyOfferedDate", from: "courseStartDate"
    end
  end

  # `Course` is an indexed type that is derived entirely from `StudentCourseEnrollment` events.
  schema.object_type "Course" do |t|
    t.field "id", "ID"
    t.field "name", "String"
    t.field "students", "[String!]!"
    t.field "firstOfferedDate", "Date"
    t.field "mostRecentlyOfferedDate", "Date"

    t.index "courses"
  end
end

Parameters:

  • name (String)

    name of the derived type

  • from_id (String)

    path to the source type field with ‘id` values for the derived type

  • route_with (String, nil) (defaults to: nil)

    path to the source type field with values for shard routing on the derived type

  • rollover_with (String, nil) (defaults to: nil)

    path to the source type field with values for index rollover on the derived type

Yields:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 131

def derive_indexed_type_fields(
  name,
  from_id:,
  route_with: nil,
  rollover_with: nil,
  &block
)
  Indexing::DerivedIndexedType.new(
    source_type: self,
    destination_type_ref: schema_def_state.type_ref(name).to_final_form,
    id_source: from_id,
    routing_value_source: route_with,
    rollover_timestamp_value_source: rollover_with,
    &block
  ).tap { |dit| derived_indexed_types << dit }
end

#derived_indexed_typesArray<Indexing::DerivedIndexedType>

Returns list of derived types for this source type.

Returns:



149
150
151
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 149

def derived_indexed_types
  @derived_indexed_types ||= []
end

#fields_with_sourcesObject



224
225
226
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 224

def fields_with_sources
  indexing_fields_by_name_in_index.values.reject { |f| f.source.nil? }
end

#index(name, **settings) {|Indexing::Index| ... } ⇒ void

Note:

Use #root_query_fields on indexed types to name the field that will be exposed on ‘Query`.

Note:

Indexed types must also define an ‘id` field, which ElasticGraph will use as the primary key.

Note:

Datastore index settings can also be defined (or overridden) in an environment-specific settings YAML file. Index settings that you want to configure differently for different environments (such as ‘index.number_of_shards`—-production and staging will probably need different numbers!) should be configured in the per-environment YAML configuration files rather than here.

This method returns an undefined value.

Converts the current type from being an embedded type (that is, a type that is embedded within another indexed type) to an indexed type that resides in the named index definition. Indexed types are directly indexed into the datastore, and will be queryable from the root ‘Query` type.

Examples:

Define a ‘campaigns` index

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

    t.index(
      "campaigns",
      # Configure `index.refresh_interval`.
      refresh_interval: "1s",
      # Use `index.search` to log warnings for any search query that take more than five seconds.
      search: {slowlog: {level: "WARN", threshold: {query: {warn: "5s"}}}}
    ) do |i|
      # The index can be customized further here.
    end
  end
end

Parameters:

Yields:



65
66
67
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 65

def index(name, **settings, &block)
  indices.replace([Indexing::Index.new(name, settings, schema_def_state, self, &block)])
end

#indexed?Boolean

Returns true if this type has an index.

Returns:

  • (Boolean)

    true if this type has an index



77
78
79
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 77

def indexed?
  indices.any?
end

#indicesObject

List of indices. (Currently we only store one but we may support multiple in the future).



72
73
74
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 72

def indices
  @indices ||= []
end

#initialize(*args, **options) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
29
30
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 22

def initialize(*args, **options)
  super(*args, **options)
  self. = {}
  yield self

  # Freeze `indices` so that the indexable status of a type does not change after instantiation.
  # (That would cause problems.)
  indices.freeze
end

#plural_root_query_field_nameString

Returns the plural name of the entity; used for the root ‘Query` field that queries documents of this indexed type.

Returns:

  • (String)

    the plural name of the entity; used for the root ‘Query` field that queries documents of this indexed type



207
208
209
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 207

def plural_root_query_field_name
  @plural_root_query_field_name || naively_pluralize_type_name(name)
end

#root_query_fields(plural:, singular: nil) {|SchemaElements::Field| ... } ⇒ void

This method returns an undefined value.

Determines what the root ‘Query` fields will be to query this indexed type. In addition, this method accepts a block, which you can use to customize the root query field (such as adding a GraphQL directive to it).

Examples:

Set ‘plural` and `singular` names

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

    # Results in `Query.people` and `Query.personAggregations`.
    t.root_query_fields plural: "people", singular: "person"

    t.index "people"
  end
end

Customize ‘Query` fields

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

    t.root_query_fields plural: "people", singular: "person" do |f|
      # Marks `Query.people` and `Query.personAggregations` as deprecated.
      f.directive "deprecated"
    end

    t.index "people"
  end
end

Parameters:

  • plural (String)

    the plural name of the entity; used for the root ‘Query` field that queries documents of this indexed type

  • singular (String, nil) (defaults to: nil)

    the singular name of the entity; used for the root ‘Query` field (with an `Aggregations` suffix) that queries aggregations of this indexed type. If not provided, will derive it from the type name (e.g. converting it to `camelCase` or `snake_case`, depending on configuration).

Yields:

  • (SchemaElements::Field)

    field on the root ‘Query` type used to query this indexed type, to support customization



200
201
202
203
204
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 200

def root_query_fields(plural:, singular: nil, &customization_block)
  @plural_root_query_field_name = plural
  @singular_root_query_field_name = singular
  @root_query_fields_customizations = customization_block
end

#root_query_fields_customizationsObject



219
220
221
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 219

def root_query_fields_customizations
  @root_query_fields_customizations
end

#runtime_metadata(extra_update_targets) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 154

def (extra_update_targets)
  SchemaArtifacts::RuntimeMetadata::ObjectType.new(
    update_targets: derived_indexed_types.map(&:runtime_metadata_for_source_type) + [self_update_target].compact + extra_update_targets,
    index_definition_names: indices.map(&:name),
    graphql_fields_by_name: ,
    elasticgraph_category: nil,
    source_type: nil,
    graphql_only_return_type: graphql_only?
  ).with(**)
end

#singular_root_query_field_nameString

Returns the singular name of the entity; used for the root ‘Query` field (with an `Aggregations` suffix) that queries aggregations of this indexed type. If not provided, will derive it from the type name (e.g. converting it to `camelCase` or `snake_case`, depending on configuration).

Returns:

  • (String)

    the singular name of the entity; used for the root ‘Query` field (with an `Aggregations` suffix) that queries aggregations of this indexed type. If not provided, will derive it from the type name (e.g. converting it to `camelCase` or `snake_case`, depending on configuration).



214
215
216
# File 'lib/elastic_graph/schema_definition/mixins/has_indices.rb', line 214

def singular_root_query_field_name
  @singular_root_query_field_name || to_field_name(name)
end