Creating graph indexes 

Creating indexes for a graph.

Creating indexes for a graph can be accomplished with many different characteristics. All indexing identifies a vertex label and a property to index. Edge indexes additionally identify an edge label.
Note: A property key can be used in more than one vertex label, as shown with the property key name below. Graph traversals will only use indexes if both the vertex label and property key are specified, as shown in Using indexes. Indexing that spans all vertex labels in a graph is not supported in DSE Graph if full graph scans are disabled.

Procedure

Secondary index

  • Create a secondary index.
    schema.vertexLabel('recipe').index('byRecipe').secondary().by('name').add()

    Identify the vertex label and property key for the index, in the vertexLabel() and by() steps, respectively. In the index() step, name the index. The secondary() step identifies the index as a secondary index.

Materialized index

  • Create a materialized view index.
    schema.vertexLabel('author').index('byAuthor').materialized().by('name').add()

    Identify the vertex label and property key for the index, in the vertexLabel() and by() steps, respectively. In the index() step, name the index. The materialized() step identifies the index as a materialized view index.

Search index

  • Create a search index. This search index has one property key indexed. If multiple property keys are indexed, chain additional by() steps.
    schema.vertexLabel('recipe').index('search').search().by('instructions').asText().add()

    Identify the vertex label and property keys for the index, in the vertexLabel() and by() steps, respectively. In the index() step, name the index search; only this naming convention can be used. The search() step identifies the index as a search index. This index is searched using full text index.

    Note: Only one search index can be created per vertex label.
  • A search index can also specify string indexing option. This example identifies a string index.
    schema.vertexLabel('recipe').index('search').search().by('instructions').asString().add()

    asString() is also an available search index option.

  • More commonly, a search index will specify multiple columns:
    schema.vertexLabel('recipe').index('search').search().by('instructions').asText().by('name').asString().add()
    Note: Prior to DSE 5.1, search indexes defaults to asText() for textual property data, if not specified as asString().
  • Search indexes can also include non-text data types:
    schema.vertexLabel('recipe').index('search').search().by('year').by('name').asString().add()
    Data types other than text are inferred from the schema and DSE Search uses a comparable Solr data type. In this example, year is indexed as an integer.
    CAUTION:
    The Decimal data type will index as a SolrDecimalStrField. Use Int, Long, Float, or Double to ensure that the Solr data types are used for sorting and range querying.
  • Create a search index for geospatial data:
    schema.propertyKey("coordinates").Point().single().create()
    schema.propertyKey("name").Text().single().create()
    
    schema.vertexLabel("place").properties("coordinates", "name").create()
    schema.vertexLabel("place").index("search").search().by("name").asText().by("coordinates").add()
    

    In this example, the property coordinates is a point defining a longitude and latitude. The search index includes coordinates without a qualifying asText() or asString() method. See Geospatial Schema for additional information.

  • Create a search index for timestamp data:
    schema. propertyKey('review_ts).Timestamp().create()
    schema.propertyKey('name').Text().create()
    schema.vertexLabel('rating').properties('name', 'review_ts').create()
    schema.vertexLabel('rating').index('search').search().by('name','review_ts').add()

Edge index

  • Create an edge index. Edges indexes are vertex-centric to a particular vertex label. For instance, the example below indexes anything that a reviewer rates.
    schema.vertexLabel('reviewer').index('ratedByStars').outE(rated).by('stars').add()

    Identify the vertex label and property keys for the index, in the vertexLabel() and by() steps, respectively. In the index() step, name the index. The outE() step is used to define the direction of the edge.

  • Create an edge index that indexes both incoming and outgoing edges:
    schema.vertexLabel('reviewer').index('ratedByStars').bothE(rated).by('stars').add()

    Identify the vertex label and property keys for the index, in the vertexLabel() and by() steps, respectively. In the index() step, name the index. The bothE() step is used to define the direction of the edge.

Property index

  • Create a property index. Property indexes are vertex-centric to a particular vertex label.
    schema().vertexLabel('author').index('byLocation').property('country').by('livedIn').add()

    Identify the vertex label and property keys for the index, in the vertexLabel() and property() steps, respectively. In the index() step, name the index. The by() step is used to define the meta-property of the property.

    .

Verifying and identifying index schema

  • Verify the index creation.
    schema.describe()
  • Display more information about the indexes by specifying the vertex label and using the describe() step. Schema that can be used to create the indexes will be displayed.
    schema().vertexLabel('author').describe()
    ==>schema.vertexLabel("author").properties("name", "gender", "nationality").create()
    schema.vertexLabel("author").index("byName").secondary().by("name").add()
    schema.vertexLabel("author").index("byAuthor").materialized().by("name").add()
  • Display information about all of the indexes using a schema traversal that filters for all vertexIndex.
    schema.traversal().V().hasLabel('vertexIndex').valueMap()
    ==>{name=[byName], type=[Secondary]}
    ==>{unique=[false], name=[byIngredient], type=[Materialized]}
    ==>{unique=[false], name=[byReviewer], type=[Materialized]}
    ==>{unique=[false], name=[byRecipe], type=[Materialized]}
    ==>{unique=[false], name=[byMeal], type=[Materialized]}
  • Get a count of the number of indexes using a schema traversal that filters for all vertexIndex.
    schema.traversal().V().hasLabel('vertexIndex').count()
    ==>5