Creating graph indexes
Creating indexes for a graph.
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()
andby()
steps, respectively. In theindex()
step, name the index. Thesecondary()
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()
andby()
steps, respectively. In theindex()
step, name the index. Thematerialized()
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()
andby()
steps, respectively. In theindex()
step, name the indexsearch
; only this naming convention can be used. Thesearch()
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 toasText()
for textual property data, if not specified asasString()
. -
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:TheDecimal
data type will index as aSolrDecimalStrField
. UseInt
,Long
,Float
, orDouble
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()
orasString()
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()
andby()
steps, respectively. In theindex()
step, name the index. TheoutE()
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()
andby()
steps, respectively. In theindex()
step, name the index. ThebothE()
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()
andproperty()
steps, respectively. In theindex()
step, name the index. Theby()
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