Sony Arouje

a programmer's log

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

[TestCase]
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.

[TestCase]
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.

[TestCase]
public void Search_Index()
{
    Index fav = Index.Get("Favorites");
    IndexQuery qry = new IndexQuery();
    qry.GetKey("FirstName").StartsWith("Vi").OR().GetKey("FirstName").Equals("Sony");
    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

[TestCase]
public void Remove_Node_FromIndex()
{
    Index fav = Index.Get("Favorites");
    Node node = Node.Get(1);
    fav.RemoveNode(node);
}

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.

[TestCase]
public void Get_Out_Nodes()
{
    GermlinPipe germlinQuery = new GermlinPipe();
    germlinQuery.G.V.Out("son");
    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.

[TestCase]
public void REST_Traversal_Test()
{
    Node node = Node.Get(19);
    Assert.IsNotNull(node);
    RestTraversal r = new RestTraversal();
    r.Order(OrderType.breadth_first)
        .Filter(new PropertyFilter().SetPropertyName("FirstName").Contains("Viji"))
        .RelationShips(RelationshipDirection.out_direction, "wife")
        .RelationShips(RelationshipDirection.all_direction, "family")
        .Uniqueness(UniquenessType.node_global)
        .MaxDepth(2);
    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 , , , ,

8 Responses

Subscribe to comments with RSS.

  1. […] Neo4jD Part 3 […]

  2. Hello – I was wondering if you have yet implemented a way to find a the path between two nodes in the graph, such as:

    http://api.neo4j.org/current/org/neo4j/graphalgo/PathFinder.html#findAllPaths%28org.neo4j.graphdb.Node,%20org.neo4j.graphdb.Node%29

    Mark

    April 10, 2012 at 5:03 am

    • Hi Mark,
      This functionality is not yet implemented. Thanks for notifying that, I will work on it and will update you.

      Regards,
      Sony Arouje

      Sony Arouje

      April 10, 2012 at 10:54 am

      • Thanks for the reply – the inclusion of the graph alogorithms for finding paths between two nodes is the only thing stopping me from considering using Neo4JD for an upcoming project.

        Thanks again!

        Mark

        April 10, 2012 at 7:53 pm

      • Hi Mark,
        Unfortunately my schedule is hectic but I will try to include this feature ASAP.

        Regards,
        Sony Arouje

        Sony Arouje

        April 11, 2012 at 11:58 am

    • Hi Mark,
      As per your request I added the functionality to find the path between two node. Here is how you can get it

      Node father = Node.Get(1);
      Node mother = Node.Get(2);

      IList relationShips = father.GetAllPathsTo(mother);
      Assert.IsNotNull(relationShips);
      Assert.AreEqual(1, relationShips.Count);
      foreach (Relationship relation in relationShips)
      Assert.AreEqual(“wife”, relation.GetType());

      The changes are checked in to Github as well as a new package is added to Nuget.

      Regards,
      Sony Arouje

      Sony Arouje

      April 12, 2012 at 12:30 am

      • Thanks for the quick turn around! I look forward to giving it a try.

        Mark

        April 12, 2012 at 9:18 am

      • I am not sure whether the new functionality satisfy your requirement, let’s give a try and let me know your feedback.

        Sony Arouje

        April 12, 2012 at 11:04 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: