Database Reference
In-Depth Information
// Get the reference of Graph Service
GraphDatabaseService graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabase(DBLocation);
//Start the Transaction.
//try-with-resources is available only with Java 1.7 and
above.
try (Transaction tx = graphDb.beginTx()) {
// Getting reference of Index Manager API
IndexManager indexMgr = graphDb.index();
// Creating New Index for Nodes and Relationship
Index<Node> actorsIndex =
indexMgr.forNodes("ArtistIndex");
RelationshipIndex rolesIndex =
indexMgr.forRelationships("RolesIndex");
tx.success();
tx.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The preceding code will create two empty indexes, one called ArtistIndex for index-
ing nodes and the other called RolesIndex . Now, for indexing nodes and relationships,
you need to manually add or remove each of the index entries by using methods provided
in org.neo4j.graphdb.index.Index or
org.neo4j.graphdb.index.RelationshipIndex API.
Let's continue the preceding example and add nodes to the ArtistIndex . Add the fol-
lowing code snippet just before tx.success();
// Creating a Node
Node node = graphDb.createNode();
// Adding Label and properties to Node
node.addLabel(DynamicLabel.label("Actors"));
node.setProperty("Name", "Ralph Macchio");
// Adding Node and its properties to the Index
actorsIndex.add(node, "Name", node.getProperty("Name"));
Search WWH ::




Custom Search