Using indexes

Using indexes for graph queries.

Indexes will be used in graph traversal queries for the first traversal step reached after theV() step, and is used to trim down the number of vertices that are initially fetched. In general, the traversal step will involve a vertex label and can include a property key and a particular property value. In a traversal, the step following g.V() is generally the step in which an index will be consulted. If a mid-traversal V() step is called, then an additional indexed step can be consulted to narrow the list of vertices that will be traversed.

Note: Graph traversals will only use indexes if the both the vertex label and property key are specified. If both are not specified, indexing will not be used and a full graph scan for the property key can result. If full graph scan is disabled, a query will fail, as shown in this example where a property is specified, but a vertex label is not specified:
g.V().has('name','Julia Child')
Could not find an index to answer query clause and graph.allow_scan is disabled: 
((label = FridgeSensor & name WITHIN [Julia Child]) | (label = author & name WITHIN [Julia Child]) | 
(label = book & name WITHIN [Julia Child]) | (label = ingredient & name WITHIN [Julia Child]) | 
(label = meal & name WITHIN [Julia Child]) | (label = recipe & name WITHIN [Julia Child]) | 
(label = reviewer & name WITHIN [Julia Child]))


The graph traversal shown uses an index to discover certain author vertices to start the query.
g.V().has('author', 'name', 'Emeril Lagasse').out('created').values('name')

This graph traversal uses an index, if the index exists, because the traversal step has('author', 'name', 'Emeril Lagasse') identifies the vertex label and the property key indexed. After finding the initial vertex to traverse from, the outgoing created edges are walked and the adjacent vertices are listed by name. This graph traversal shows the importance of using the vertex label in combination with the property key, as two different elements, authors and recipes, use the same property key name.

Checking for the use of indexing can be accomplished with the profile() method:
gremlin> g.V().has('author', 'name', 'Emeril Lagasse').out('created').values('name').profile()
==>Traversal Metrics
Step                                                               Count  Traversers       Time (ms)    % Dur
DsegGraphStep([~label.=(author), name.=(Emeril ...                     1           1           2.196    51.37
  query-optimizer                                                                              0.199
  query-setup                                                                                  0.004
  index-query                                                                                  0.946
DsegVertexStep(OUT,[created],vertex)                                   2           2           0.935    21.88
  query-optimizer                                                                              0.101
  query-setup                                                                                  0.000
  vertex-query                                                                                 0.282
DsegPropertiesStep([name],value)                                       2           2           1.030    24.11
  query-optimizer                                                                              0.044
  query-setup                                                                                  0.005
  vertex-query                                                                                 0.347
  vertex-query                                                                                 0.639
  query-setup                                                                                  0.000
NoOpBarrierStep(2500)                                                  2           2           0.113     2.64
                                            >TOTAL                     -           -           4.276        -
Note the index-query used in the first step DsegGraphStep. If an index was not used, index-query would be missing from the profile output.