Domanda

In Titan, I create an index using:

graph.makeKey("name").dataType(String.class).indexed(Vertex.class).indexed(Edge.class).unique().make();

How can I do this in Neo4j using the Java API?

È stato utile?

Soluzione

In Cypher you would create a label based constraint on the label :Person and the property name like this.

CREATE CONSTRAINT ON (p:Person) ASSERT p.name IS UNIQUE;

In the Java API it is

try (Transaction tx = db.beginTx()) {      
  db.schema().constraintFor(DynamicLabel.label("Person")).assertPropertyIsUnique("name").create();
  tx.success();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top