Reputation: 2340
I've recently started using Neo4JClient for my .NET integration and I have (eventually) figured most things out. However, this one's stumped me.
I'm interested in getting paths back from a Cypher query, preferably in a POCO object so I can work on a front end for these queries.
So my question essentially is how do I do this in Neo4JClient? And if I can't, do any of the other Neo4J .NET clients support this?
A sample cypher query:
start n = node:idx(id="{id}")
MATCH p=(n)-[:RELATED_TO*0..3]-()
RETURN p;
So, I want all nodes around a specific node with both incoming and outgoing relationships to a depth of 3. There's another type of query too, but it uses withs and I need to find out if/how Neo4JClient supports that (another question there though).
Up until now, I've been using Gremlin with open and closed sets to populate a custom object with information on it's sub-relationships. It's by no means efficient, hence why I'd like to do it with paths somehow. The customobject in question looks a little like this.
public class ConnectedNode : BaseNode
{
public List<NodeRelation> RelatedNodes { get; set; }
public ConnectedNode()
{
RelatedNodes = new List<NodeRelation>();
}
}
public class NodeRelation
{
// ... various properties for relationship payload type stuff
public ConnectedNode RelatedNode { get; set; }
public RelationshipDirection Direction { get; set; }
}
I'm happy for anything that pulls back the node and relationship data on the path I'm looking for as long as it's efficient.
Upvotes: 2
Views: 726
Reputation: 4235
I started with Chris Skardon's code (http://geekswithblogs.net/cskardon/archive/2013/07/23/neo4jclient-ndash-getting-path-results.aspx) and just started to remove the Relationship classes to see if it would work. I like Chris' solution because it returns the node properties as well as the relationship (including TypeKey) in one go. Turns out you don't need to create any new classes.
var queryResults = graphClient.Cypher
.Match("p=(n)-[:RELATED_TO|OTHER_RELATION*0..2]-()")
.Return(p => new
{
Nodes = Return.As<IEnumerable<Node<**YOUR_NODE_CLASS**>>>("nodes(p)"),
Relationships = Return.As<IEnumerable<RelationshipInstance<Dictionary<string,string>>>>("rels(p)")
})
.Results;
resultsPath.Dump();//Use LINQPad to see the results
The relationship object that I used is Dictionary<string,string>
(which you can use in place of **YOUR_NODE_CLASS**
) - for me, there is no data in the RelationshipReference.Data
field, but I think there would be if my relationship had properties.
Upvotes: 1
Reputation: 2340
I ended up working out the answer to this problem and have written a blog post on the subject. But so as you don't have to read the post for the jist of it...
First, extract the nodes and relations of the path using the EXTRACT function. This will give you a 2-column result. Encompass this 2-columned result in a POCO of your own choosing that takes a list of nodes and RelationshipInstances. Then retrieve your query into this kind of object using a Projection. Voila!
Hope this helps, it helped me.
Upvotes: 1
Reputation: 6280
Have you tried:
ICypherFluentQueryReturned<PathsResult> query
= graphClient.Cypher
.StartWithNodeIndexLookup("n", "idx", "id", id)
.Match("p=(n)-[:RELATED_TO*0..2]-()")
.Return<PathsResult>("p");
IEnumerable<PathsResult> res = query.Results;
Each of the results in res
should be what you're after?
Upvotes: 1