neo4j - neo4jclient: create and related multiple node to another node -
it's hard ask question, not sure if have picked right title it. please bare me.
imagine classes in c#:
public class user { public int id { get; set; } public string username { get; set; } } public class post { public int id { get; set; } public string text { get; set; } public string[] hashtags { get; set; } } each user can add post , relation between them author, each post have array of hash-tags each of them going separate node in graph.
when going save each post, find user in grph, create post node , relate them author relationship.
the question how can create , relate each hashtag post in same query. (to inside transaction).
how dynamically add item query create it. problem not create node , relation in 1 line of create.
here have tried far:
var cypherquery = db.instance.cypher .match("(user:user)") .where((user user) => user.username == "xyz") .create("user-[:author]->(post:post {newpost})") .withparam("newpost", new post() {id = 1, text = "here post #somehashtag"}); //how relate node number of hashtags in post object??? cypherquery.executewithoutresults(); is in single query or should divide in multiple round trips.
i have tried foeach seams post not have value inside foreach loop:
i have tried this:
var cyphercommand = db.instance.cypher .match("(user:user)") .where((user user) => user.username == "farvashani") .create("user-[:author]->(post:post {newpost})") .withparam("newpost", "here post @tag1 , tag2") .foreach(@"(hashtag in {hashtags}| merge post-[:mentioned]->(hash:hashtag {text: hashtag}))") .withparam("hashtags", new string[] {"tag1", "tag2"}); cyphercommand.executewithoutresults(); any appreciated.
in opinion, think have pre-process text property first.
string text = ""here post #somehashtag""; // example list<string> hashtags = new list<string>(); int cnt = 0; foreach (match match in regex.matches(text, @"(?<!\w)#\w+")) { hashtags.add(match.value); } then create new instance of post:
post newpost = new post { id = 1, text = "here post #somehashtag", hashtags = hashtags }; so, can use cypher:
var cyphercommand = db.instance.cypher .match("(user:user)") .where((user user) => user.username == "farvashani") .create("user-[:author]->(post:post {newpost})") .withparam(new {newpost}).executewithoutresults(); hope help.
p/s: ask question? think better retrieve separated graph if use each hashtag label of post? newpost:somehashtag example?
Comments
Post a Comment