user2275688
user2275688

Reputation: 111

Creating a node ID and properties dynamically in NEO4J using .Net client

How can I create using .Net client a node and its attributes dynamically? Some thing like this?

  Node node = new Node();
  node.AddProperty("Type", "Domain");

I do not want to hard code this in a class i.e.

  JsonProperty***("Bar")]***
  public string Foo { get; set; }

  var myNodeReference = client.Create(new MyNode { Foo = "bar" });

Upvotes: 0

Views: 755

Answers (3)

Mark
Mark

Reputation: 156

You can use Cypher.Net to create dynamic nodes and relationships http://www.nuget.org/packages/CypherNet/

(Documentation and source here: https://github.com/mtranter/CypherNet)

const string NEO_SERVER_URL = "http://localhost:7474/db/data/";
var sessionFactory = Fluently.Configure(NEO_SERVER_URL ).CreateSessionFactory();
var cypherSession = sessionFactory.Create();
var node = cypherSession.CreateNode(new { FirstName = "John", LastName = "Smith"});
dynamic dynamicNode = node;
dynamicNode.SomeProperty = "SomeValue";
dynamicNode.AnotherProperty = 123;
cypherSession.Save(node);

Upvotes: 0

Kenny Bastani
Kenny Bastani

Reputation: 3308

In Neo4j server v2.0 M06 build that is available for download at the Neo4j.org download page, you are able to use JSON in a Cypher query directly to your Neo4j server.

Example:

CREATE 
    (view0:event:view 
     { 
         key:'view0', 
         verb:'view', 
         past:'has viewed', 
         present:'is viewing', 
         future:'will view' 
      })-[:event]->d16

You can grab the following C# class file from my GitHub repository that allows you to send Cypher queries directly to the Neo4j server:

https://github.com/kbastani/predictive-autocomplete/blob/master/predictive-autocomplete/PredictiveAutocomplete/CypherQueryCreator.cs

You can see its usage by going to the following C# class file from GitHub:

https://github.com/kbastani/predictive-autocomplete/blob/master/predictive-autocomplete/PredictiveAutocomplete/Processor.cs

Go down to "GetRankedNodesForQuery", I've copied the code here.

public static List<IGraphNode> GetRankedNodesForQuery(string index, string luceneQuery, string relationshipLabel, string labelPropertyName, int skip, int limit)
{
        var sb = new StringBuilder();
        sb.AppendLine("START node=node:{0}(\"{1}\")");
        sb.AppendLine("WITH node");
        sb.AppendLine("SKIP {2}");
        sb.AppendLine("LIMIT {3}");
        sb.AppendLine("WITH node");
        sb.AppendLine("MATCH n-[{4}]->node");
        sb.AppendLine("WITH node, count(distinct n) as size");
        sb.AppendLine("RETURN node.{5}? as label, size");
        sb.AppendLine("ORDER BY id(node)");
        sb.AppendLine("LIMIT {3}");

        string commandQuery = sb.ToString();

        commandQuery = string.Format(commandQuery, index, luceneQuery, skip, limit, !string.IsNullOrEmpty(relationshipLabel) ? string.Format(":{0}", relationshipLabel) : string.Empty, labelPropertyName);

        GraphClient graphClient = GetNeo4jGraphClient();

        var cypher = new CypherFluentQueryCreator(graphClient, new CypherQueryCreator(commandQuery), new Uri(Configuration.GetDatabaseUri()));

        var resulttask = cypher.ExecuteGetCypherResults<GraphNode>();
        var graphNodeResults = resulttask.Result.ToList().Select(gn => (IGraphNode)gn).ToList();
        return graphNodeResults;
}

In other versions you would need to create a wrapper. The easiest route at the moment would be to upgrade to the 2.0 version. If this is not feasible for you, please let me know and I will write the .NET C# wrapper.

Upvotes: 2

MrDosu
MrDosu

Reputation: 3435

While I tried a couple of the available .NET clients for neo4j I found them all lacking for my use case. Seeing as those clients are just wrappers around the REST API it was much easier to just talk to the server via REST directly. HTTP and JSON support is excellent at the current state of .NET.

That way you can dynamically construct your nodes and create them anyway you like (node creation via REST).

Upvotes: 0

Related Questions