Node reuse, instead of creating new ones?

我只是一个虾纸丫 提交于 2019-12-11 12:03:21

问题


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)' 

回答1:


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.



来源:https://stackoverflow.com/questions/55680683/node-reuse-instead-of-creating-new-ones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!