Node reuse, instead of creating new ones?
Asked Answered
O

1

1

I'm trying to create (action)->(state) pair in such a way so that if :

  • the action exists, use it, instead of creating new one
  • the state exists, use it, instead of creating new one

and do it in single query.

The one I have creates new action node if the state is different, from previous calls. So I end up with multiple action nodes which are the same.

    query =  "merge (:state {id:%s})-[:q {q:%s}]->(:action {id:%s})" % (state, 0, action)

I use radis-graph.


The only way is to use 3 queries instead of 1 to achieve this :

graph.query mem 'merge (:state {id:9})'
graph.query mem 'merge (:action {id:9})'
graph.query mem 'match (s:state), (a:action) where s.id = 9 and a.id = 9 create (s)-[:q {q:0.3}]->(a)' 
Obmutescence answered 14/4, 2019 at 22:42 Comment(0)
T
2

At the moment RedisGraph doesn't supports mixing the MATCH and MERGE clauses, and so you don't have much options besides splitting the query as you did, one suggestion would be to wrap those three queries within a MULTI EXEC:

MULTI
graph.query mem 'merge (:state {id:9})'
graph.query mem 'merge (:action {id:9})'
graph.query mem 'match (s:state), (a:action) where s.id = 9 and a.id = 9 create (s)-[:q {q:0.3}]->(a)'
EXEC

This should speed things up, we'll update here once MATCH and MERGE can be mixed.

Turbojet answered 16/4, 2019 at 6:43 Comment(2)
how can i do multi-exec from inside python.. is there a thing like transaction in RG, so that those 3 queries are separated from concurrently running clients ?Obmutescence
redis_con = redis.Redis() redis_con.execute_command("MULTI") redis_con.execute_command("GRAPH.QUERY", mem, "merge (:state {id:9})") redis_con.execute_command("GRAPH.QUERY", mem, "merge (:action {id:9})") redis_con.execute_command("GRAPH.QUERY", mem, "match (s:state), (a:action) where s.id = 9 and a.id = 9 create (s)-[:q {q:0.3}]->(a)") results = redis_con.execute_command("EXEC")Turbojet

© 2022 - 2024 — McMap. All rights reserved.