Sony Arouje

a programmer's log

Posts Tagged ‘Graph Traversal

Neo4jD–.NET client for Neo4j Graph Database Part 3

with 8 comments

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);

Neo4jD Source Code

Written by Sony Arouje

February 10, 2012 at 1:12 am

Posted in .NET

Tagged with , , , ,


Get every new post delivered to your Inbox.

Join 243 other followers

%d bloggers like this: