Class SearchTrait

The Elasticsearch integration trait.

Indexes Kosmos contents in Elasticsearch, then hooks into filtering queries for these fields and converts normal Gremlin queries to Neptune-specific fulltext queries.

Requires AWS Neptune and AWS OpenSearch.

Usage:

traits:
type: search
fields:
- title
- body
- data
exclude_child_properties:
- data.additionalProperties
- data.interests
index:
accessKeys:
appRole:
traversals:
- out(hasSchema).inE(permission).outV().hasLabel(AppRole)
relationships:
tags:
traversals:
- out(taggedWith).hasLabel(Tag)
activities:
traversals:
- out(hasActivity).hasLabel(Activity)
reindex_connected_vertices:
- out(taggedWith).hasLabel(Tag)

In config.yml

search:
roles:
org:
accessKeys:
custom:
appRole:
traversals:
- out(hasRole).hasLabel(AppRole)
organisation:
traversals:
- out(memberOf).hasLabel(Organisation)

service:
accessKeys:
label:
- Answer

# no access
webmaster:

admin:
fullAccess: true

Mermaid

Content indexing

%%{init:{"theme":"dark"}}%% sequenceDiagram actor User participant Kosmos participant Neptune participant Elasticsearch User->>Kosmos: Create/Update/Delete mutation Kosmos->>Neptune: Create/Update/Delete vertices activate Neptune Neptune->>Kosmos: OK deactivate Neptune Kosmos->>Elasticsearch: Create/Update/Delete document activate Elasticsearch Elasticsearch->>Kosmos: OK deactivate Elasticsearch Kosmos->>User: OK
%%{init:{"theme":"default"}}%% sequenceDiagram actor User participant Kosmos participant Neptune participant Elasticsearch User->>Kosmos: Create/Update/Delete mutation Kosmos->>Neptune: Create/Update/Delete vertices activate Neptune Neptune->>Kosmos: OK deactivate Neptune Kosmos->>Elasticsearch: Create/Update/Delete document activate Elasticsearch Elasticsearch->>Kosmos: OK deactivate Elasticsearch Kosmos->>User: OK
sequenceDiagram
    actor User
    participant Kosmos
    participant Neptune
    participant Elasticsearch
    User->>Kosmos: Create/Update/Delete mutation
    Kosmos->>Neptune: Create/Update/Delete vertices
    activate Neptune
    Neptune->>Kosmos: OK
    deactivate Neptune
    Kosmos->>Elasticsearch: Create/Update/Delete document
    activate Elasticsearch
    Elasticsearch->>Kosmos: OK
    deactivate Elasticsearch
    Kosmos->>User: OK

Mermaid

Search query

%%{init:{"theme":"dark"}}%% sequenceDiagram actor User participant Kosmos participant Neptune participant Elasticsearch User->>Kosmos: GraphQL query Kosmos->>Neptune: Gremlin query activate Neptune Neptune->>Elasticsearch: Search query activate Elasticsearch Elasticsearch->>Neptune: Content IDs deactivate Elasticsearch Neptune->>Kosmos: Gremlin & ES results intersection deactivate Neptune Kosmos->>User: GraphQL response
%%{init:{"theme":"default"}}%% sequenceDiagram actor User participant Kosmos participant Neptune participant Elasticsearch User->>Kosmos: GraphQL query Kosmos->>Neptune: Gremlin query activate Neptune Neptune->>Elasticsearch: Search query activate Elasticsearch Elasticsearch->>Neptune: Content IDs deactivate Elasticsearch Neptune->>Kosmos: Gremlin & ES results intersection deactivate Neptune Kosmos->>User: GraphQL response
sequenceDiagram
    actor User
    participant Kosmos
    participant Neptune
    participant Elasticsearch
    User->>Kosmos: GraphQL query
    Kosmos->>Neptune: Gremlin query
    activate Neptune
    Neptune->>Elasticsearch: Search query
    activate Elasticsearch
    Elasticsearch->>Neptune: Content IDs
    deactivate Elasticsearch
    Neptune->>Kosmos: Gremlin & ES results intersection
    deactivate Neptune
    Kosmos->>User: GraphQL response

Hierarchy

Implements

Constructors

Properties

_access: AccessControl
_accessKeysService: AccessKeysService
_client: Client

Trait instance configuration.

_dataSourceWrapper: DataSourceWrapper
_global: boolean = false

Whether this is a global (not bound to any specific schema item) trait instance.

_graphqlSchemaManager: GraphQLSchemaManager
_queryCache: QueryCache
_runtimeConfig: RuntimeConfig
_schemaManager: SchemaManager
_search: Search
_type?: "search"

Trait type.

userVertexLabel: string = 'User'
schemaAttached: boolean = false

Ensure that the global schema hooks only run once.

searchEnabledVertices: string[] = []

Keep track of the vertex types on which search is enabled.

synonymsEnabled: boolean = false

A flag to enable synonyms functionalities once for both the retrieval and updating of the index.

unionsAltered: boolean = false

Keep track of whether the union schema has been altered yet.

Methods

  • Helper method which initializes/returns SearchTrait storage object.

    Returns

    SearchTraitStorage

    Parameters

    Returns Record<any, any>

  • Parameters

    • filtersTree: FilterTreeExpression

    Returns string

  • Creates an edge search document in ES.

    Parameters

    • __namedParameters: ElasticsearchEdgeIndexOperationOptions

    Returns Promise<void>

  • Delete all edge documents connected to a deleted vertex.

    Ensuring that the vertex itself is deleted is the caller's responsibility.

    Parameters

    • tenant_id: string
    • vertexId: string

    Returns Promise<void>

  • Deletes a search document in ES.

    Parameters

    • options: ElasticsearchDeleteOperationOptions

    Returns Promise<void>

  • Parameters

    Returns Promise<{
        hasNextPage: boolean;
        items: any[];
    }>

  • Extract only the edge properties that are configured to be indexed.

    Returns

    A name-value record of the properties to be indexed.

    Parameters

    • edge: {
          id: string;
          inV: {
              id: string;
              label: string;
          };
          label: string;
          outV: {
              id: string;
              label: string;
          };
      }

      The edge object

      • id: string
      • inV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string
      • label: string
      • outV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string

    Returns Record<string, unknown>

  • Get the search relationships defined for this schema.

    Returns

    The relationships, as possibly altered by autoConfig.

    Returns any

  • Search field resolver.

    Parameters

    Returns Promise<null | {
        count: any;
        data: {};
        entities: undefined | any[];
        facets: undefined | string;
        relationships: undefined | any[];
        results: Record<string, any>;
    }>

  • Parameters

    • edge: {
          id: string;
          inV: {
              id: string;
              label: string;
          };
          label: string;
          outV: {
              id: string;
              label: string;
          };
      }
      • id: string
      • inV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string
      • label: string
      • outV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string
    • tenant: string
    • g: QueryFactory<any>

    Returns Promise<boolean>

  • Parameters

    • tenant: string
    • tag: string

    Returns Promise<void>

  • This handles the retrieval of the synonyms for a tenant.

    Returns

    An array of synonym strings

    Parameters

    • _parent: unknown

      The parent

    • _args: Record<string, any>
    • context: KosmosUserContext<object>

      The user context

    Returns Promise<string[]>

  • This is where any GraphQL mutation entries that are required by the trait are added.

    Returns

    Returns graphql schema segment for inclusion in main graphql doc.

    Parameters

    • schemaString: string

      {string}

    Returns string

  • The postHandleAddRelationship hook implementation, is called when an edge has been added to a vertex. As the edge may change the data we hold in Elasticsearch we need to update the documents accessKeys and relationships fields.

    Parameters

    Returns Promise<void>

  • The postHandleEntityRelationshipUpdate hook implementation, is called when an edge has been added to / removed from a vertex. As the edge may change the data we hold in OpenSearch we need to update the document's accessKeys and relationships fields.

    Parameters

    Returns Promise<void>

  • This method is triggered when a trait callback has been activated

    Type Parameters

    • RT = any

    • Payload = any

    Parameters

    • callbackName: string

      This is the name of the callback function to call.

    • datasourceWrapper: DataSourceWrapper

      References to the Graph Hanlder

    • data: TraitDataObject<RT, Payload, Promise<any>>

      Request data passed along as part of the request.

    Returns Promise<RT>

  • This method is used to retrieve additions to the GraphQL schema and to register the callbacks associated to the Vertex.

    Returns

    • GraphQL schema with trait schema appended.

    Parameters

    • schemaString: string

      Existing GraphQL schema.

    Returns string

  • Parameters

    Returns Promise<{
        hasNextPage: boolean;
        next: string;
        successful: number;
    }>

  • Parameters

    • edge: {
          id: string;
          inV: {
              id: string;
              label: string;
          };
          label: string;
          outV: {
              id: string;
              label: string;
          };
      }
      • id: string
      • inV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string
      • label: string
      • outV: {
            id: string;
            label: string;
        }
        • id: string
        • label: string
    • tenant: string
    • g: QueryFactory<any>

    Returns Promise<boolean>

  • Inherit Doc

    are not really overriding how config is set; rather, we exploit the fact that this method is called at a particular point during initialisation to keep track of the schemas on which the trait is enabled.

    Specifically: it's called after autoConfig, which decides whether the trait will be enabled on this entity type or not; but before alterUnions, which is where we need to use that information.

    Parameters

    Returns void

  • Parameters

    • type: "edge" | "vertex"
    • items: any[]

    Returns any[]

  • This handles the update functionality for the synonyms,

    • A synonyms file is created in bucket with the key prexised by tenant id.
    • The synonyms are stored as a single string with each synonym separated by a new line.

    Returns

    Success or failure

    Parameters

    • _parent: unknown

      The parent

    • args: Record<string, any>

      The synonyms to update

    • context: KosmosUserContext<object>

      The user context

    Returns Promise<boolean>

  • NOT CURRENTLY USED

    Returns

    The package details

    Parameters

    • context: KosmosUserContext<object>

      The user context

    • bucket: string

      The bucket to store the synonyms file in

    • key: string

      The key for the synonyms file

    Returns Promise<undefined | PackageDetails>

  • --- NOT CURRENTLY USED --- This function is used to update the synonyms package in OpenSearch, it uses the method of updating the analyser settings for the index.

    • The index is closed
    • The analyser settings are updated
    • The index is opened

    Once the above steps are completed the synonyms will be updated, however it will be required to re-index the data.

    Returns

    Boolean indicating success or failure

    Parameters

    • context: KosmosUserContext<object>

      The user context

    • synonyms: string[]

      The synonyms to store

    Returns Promise<boolean>

  • This function is used to update the synonyms file in the S3 bucket.

    Parameters

    • context: KosmosUserContext<object>

      The user context

    • bucket: string

      The bucket to store the synonyms file in

    • key: string

      The key for the synonyms file

    • synonyms: string[]

      The synonyms to store

    Returns Promise<void>

Generated using TypeDoc