When indexed, throw Exception during add Vertex

Viewed 290

The index has built well;
No Exception Here, and the graphManagement has committed well;

graphManagement.buildIndex("uidIndex", Vertex.class). 
     addKey(graphManagement.getPropertyKey("uid_code")).
     buildCompositeIndex();

i have configured the query.force-index=true ;
But when I try to add some vertex, using the propertyKey 'uid_code', code is following:

Vertex vertex = g.V().addV(label).
       property(propertyKey, propertyValue).
       next(); 

Exception throwed :

org.janusgraph.core.JanusGraphException: Could not find a suitable index to answer graph query and graph scans are disabled: [()]:VERTEX
    at org.janusgraph.graphdb.transaction.StandardJanusGraphTx$6.execute(StandardJanusGraphTx.java:1283)
    at org.janusgraph.graphdb.transaction.StandardJanusGraphTx$6.execute(StandardJanusGraphTx.java:1150)
    at org.janusgraph.graphdb.query.QueryProcessor$LimitAdjustingIterator.getNewIterator(QueryProcessor.java:209)
    at org.janusgraph.graphdb.query.LimitAdjustingIterator.hasNext(LimitAdjustingIterator.java:68)
    at com.google.common.collect.Iterators$7.computeNext(Iterators.java:650)
    at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
    at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
    at org.janusgraph.graphdb.query.ResultSetIterator.nextInternal(ResultSetIterator.java:54)
    at org.janusgraph.graphdb.query.ResultSetIterator.<init>(ResultSetIterator.java:44)
    at org.janusgraph.graphdb.query.QueryProcessor.iterator(QueryProcessor.java:68)
    at com.google.common.collect.Iterables$7.iterator(Iterables.java:613)
    at org.janusgraph.graphdb.tinkerpop.optimize.JanusGraphStep.lambda$new$0(JanusGraphStep.java:71)
    at org.apache.tinkerpop.gremlin.process.traversal.step.map.GraphStep.processNextStart(GraphStep.java:142)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.hasNext(AbstractStep.java:143)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.ExpandableStepIterator.next(ExpandableStepIterator.java:50)
    at org.apache.tinkerpop.gremlin.process.traversal.step.map.MapStep.processNextStart(MapStep.java:36)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:128)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep.next(AbstractStep.java:38)
    at org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.next(DefaultTraversal.java:200)
    at dataTrans.KfkMsgParser.createMerge(KfkMsgParser.java:742)   

Can someone tell me whether i forget something in document ?
Or
what i can do to fix this problem ?

2 Answers

To add a single vertex, you should invoke addV() directly on the traversal source g:

 String propertyKey = "uid_code";
 Vertex vertex = g.addV(label).
     property(propertyKey, propertyValue).
     next();

When you start the query with g.V().addV(label)..., the traversal attempts to scan all vertices with g.V() and thus the exception is thrown.

Your index should be enabled to be used.

graph.tx().rollback(); //Never create new indexes while a transaction is active
JanusGraphManagement graphManagement = graph.openManagement();
graphManagement.buildIndex("uidIndex", Vertex.class). 
     addKey(graphManagement.getPropertyKey("uid_code")).
     buildCompositeIndex();
graphManagement.commit();

//Wait for the index to become available
ManagementSystem.awaitGraphIndexStatus(graph, "uidIndex").call();

graphManagement = graph.openManagement();
// Use REINDEX to reindex old data and enable the index
// Use ENABLE to simply enable the index
graphManagement.updateIndex(graphManagement.getGraphIndex("uidIndex"), SchemaAction.REINDEX).get();
graphManagement.commit();

You can check index lifecycle here: https://github.com/JanusGraph/janusgraph/wiki/Indexing

Here is the index documentation: https://docs.janusgraph.org/latest/indexes.html

Related