简体   繁体   中英

cyclic relationships between nodes in neo4j

i'm working with neo4j and python (with py2neo), i need to put something like this, http://i.imgur.com/1rLOdiq.png , in the database. Currently i have this, http://i.imgur.com/wxsY6YO.png , with this cypher query

grafoNeo4j.cypher.execute("merge (n1:k_mer {name : '"+src.k_1_mer+"'}) 
merge (n2:k_mer {name : '"+dst.k_1_mer+"'}) merge (n1)-[:solapa]-(n2)")

where:

  • grafoNeo4j , is my graph in the db.
  • src.k_1_mer and dst.k_1_mer , are the nodes name

my idea is to get the graph of the first image, wit the corresponding cyclic relationships in the database. Hope that this is clear enough. Thanks for your time.

So, this is simple one.

This is query for the picture above:

CREATE (long_:Node {name: "long_"})
CREATE (ong_t:Node {name: "ong_t"})
CREATE (ng_ti:Node {name: "ng_ti"})
CREATE (g_tim:Node {name: "g_tim"})
CREATE (_time:Node {name: "_time"})
CREATE (ong_l:Node {name: "ong_l"})
CREATE (ng_lo:Node {name: "ng_lo"})
CREATE (g_lon:Node {name: "g_lon"})
CREATE (_long:Node {name: "_long"})
CREATE (a_lon:Node {name: "a_lon"})

CREATE (_long)-[:solapa]->(long_)
CREATE (_long)-[:solapa]->(long_)
CREATE (_long)-[:solapa]->(long_)
CREATE (a_lon)-[:solapa]->(_long)
CREATE (long_)-[:solapa]->(ong_l)
CREATE (long_)-[:solapa]->(ong_l)
CREATE (long_)-[:solapa]->(ong_t)
CREATE (ong_t)-[:solapa]->(ng_ti)
CREATE (ng_ti)-[:solapa]->(g_tim)
CREATE (g_tim)-[:solapa]->(_time)
CREATE (ong_l)-[:solapa]->(ng_lo)
CREATE (ong_l)-[:solapa]->(ng_lo)
CREATE (ng_lo)-[:solapa]->(g_lon)
CREATE (ng_lo)-[:solapa]->(g_lon)
CREATE (g_lon)-[:solapa]->(_long)
CREATE (g_lon)-[:solapa]->(_long)

You can just copy-paste it to Neo4j browser to verify.

Result:

结果

Note: be careful with MERGE statements, do not overuse them when they are not needed.

It is a waste of resources to have identical copies of the same relationship between 2 nodes. You can instead maintain a count property on a single relationship. The optional ON CREATE and ON MATCH subclauses for the MERGE clause make this very easy.

For example:

MERGE (n1:k_mer {name : 'a'}) 
MERGE (n2:k_mer {name : 'b'})
MERGE (n1)-[r:solapa]-(n2)
ON CREATE SET r.cnt = 1
ON MATCH SET r.cnt = r.cnt + 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM