Creating graph indexes
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.
A property key can be used in more than one vertex label, as shown with the property key |
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.
+ .Creating an index image::screenshots/createIndex1.png[createIndex1,550]
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.
+ .Create an index part two image::screenshots/createIndex2.png[createIndex2,550]
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.
+
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()
In DSE 5.1 and later, textual search indexes are by default indexed in both tokenized (
TextField
) and non-tokenized (StrField
) forms. This means that all textual predicates (token
,tokenPrefix
,tokenRegex
,eq
,neq
,regex
,prefix
) will be usable with all textual vertex properties created. Practically, search indexes should be created using theasString()
method only in cases where there is absolutely no use for tokenization and text analysis, such as for inventory categories (silverware, shoes, clothing). TheasText()
method is used if searching tokenized text, such as long multi-sentence descriptions. The query optimizer will choose whether to use analyzed or non-analyzed indexing based on the textual predicate used.Prior to DSE 5.1, search indexes defaulted to
asText()
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.The
Decimal
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()
Create an index part 3 -
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