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)' 
sten
  • 7,028
  • 9
  • 41
  • 63

1 Answers1

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.

SWilly22
  • 869
  • 4
  • 5
  • 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 ? – sten Apr 16 '19 at 17:45
  • 1
    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") – SWilly22 Apr 18 '19 at 07:35