Over a million developers have joined DZone.

Neo4jD - .NET CLient for a Neo4j Graph Database - Part 3

DZone's Guide to

Neo4jD - .NET CLient for a Neo4j Graph Database - Part 3

· Database Zone
Free Resource

Learn how to create flexible schemas in a relational database using SQL for JSON.

This post talks about Index and Graph traversal functionalities added to Neo4jD.

Neo4j supports Cypher, Germlin and REST based api’s for traversal. As of now Neo4jD can creates query for Germlin and REST based traversal. Traversal is not fully implemented in Neo4jD, it’s still in progress.

How to Create Index

Creating index in Neo4j using Neo4jD is very simple as shown below

public void Create_Index()
    Index test = Index.Create("TestIndex");
    Index fav = Index.Create("Favorites");

Indexing nodes with key/value pair helps to search it faster. Let’s see how to add nodes to the Favorites index.

public void AddNodeToFavorites()
    Index fav = Index.Get("Favorites");
    Node node = Node.Get(1);
    fav.Add(node, "FirstName", "Sony");

    Node node1 = Node.Get(2);
    fav.Add(node1, "FirstName", "Viji");

We added two nodes to the index Favorites, as I said using index we can easily retrieve Nodes using the Key/Value pair. Let’s see how we can query the index using Neo4jD.

public void Search_Index()
    Index fav = Index.Get("Favorites");
    IndexQuery qry = new IndexQuery();
    IList<Node> nodes= fav.Search(qry);
    Assert.AreEqual(2, nodes.Count);

You decided to remove a Node from the Favorites index, it’s a very simple call as shown below

public void Remove_Node_FromIndex()
    Index fav = Index.Get("Favorites");
    Node node = Node.Get(1);


I removed Node Sony from the index.

That’s all about Index, let’s go through Germlin and REST Traversal

Germlin Traversal

Germlin is a groovy based Graph traversal language, Neo4j has a Germlin plugin to send Germlin script to Neo4j server.

Let’s see how to create a Germlin query using Neo4jD.

public void Get_Out_Nodes()
    GermlinPipe germlinQuery = new GermlinPipe();
    Node father = Node.Get(1);
    IList<Node> nodes = father.Filter(germlinQuery);
    Assert.AreEqual(1, nodes.Count);

For more Germlin query you can reffer Neo4j API reference.

REST Traversal

For Rest traversal we need to provide Json structured query to the server as shown below. For more details goto Neo4j REST API reference.

  “order” : “breadth_first”,
  “return_filter” : {
    “body” : “position.endNode().getProperty(‘name’).toLowerCase().contains(‘t’)”,
    “language” : “javascript”
  “prune_evaluator” : {
    “body” : “position.length() > 10″,
    “language” : “javascript”
  “uniqueness” : “node_global”,
  “relationships” : [ {
    "direction" : "all",
    "type" : "knows"
  }, {
    "direction" : "all",
    "type" : "loves"
  } ],
  “max_depth” : 3

To create the syntax Neo4jD uses a fluent API as shown below.

public void REST_Traversal_Test()
    Node node = Node.Get(19);
    RestTraversal r = new RestTraversal();
        .Filter(new PropertyFilter().SetPropertyName("FirstName").Contains("Viji"))
        .RelationShips(RelationshipDirection.out_direction, "wife")
        .RelationShips(RelationshipDirection.all_direction, "family")
    IList<Node> nodes = node.Filter(r);
    Assert.AreEqual (1, nodes.Count);

Create flexible schemas using dynamic columns for semi-structured data. Learn how.


Published at DZone with permission of Sony Arouje, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}