DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Neo4jD - .NET CLient for a Neo4j Graph Database - Part 3

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

Sony Arouje user avatar by
Sony Arouje
·
Mar. 04, 12 · Database Zone · Interview
Like (0)
Save
Tweet
5.54K Views

Join the DZone community and get the full member experience.

Join For Free

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


Neo4j Database Graph (Unix)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Anypoint CLI Commands in MuleSoft
  • The Developer's Guide to SaaS Compliance
  • Implementing Microservices Architectures
  • Kotlin vs Java: Which One Is the Best?

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo