Neo4J REST API cypher-查询使用MERGE Con​​struct

时间:2017-03-20 13:02:31

标签: rest neo4j cypher

我正在尝试在它们之间创建节点和关系。以下是我使用的代码。

String cypherUri = "http://localhost:7474/" + "cypher";        
JSONObject Neo4jQueryJSON = new JSONObject();
HashMap<String, String> params = new HashMap<String, String>();

params.put("value","H090");
params.put("color","red"); 

String query = "CREATE (n:color {color: {name} }), (m:value {value: {name} }), (n)-[:hasVALUE]->(m)";
Neo4jQueryJSON.put("query", query);
Neo4jQueryJSON.put("params", params);                   


WebResource resource = Client.create().resource( cypherUri );
ClientResponse response =
                   resource.accept(MediaType.APPLICATION_JSON_TYPE)
                           .type(MediaType.APPLICATION_JSON_TYPE)                               
                           .header("X-Stream","true")
                           .post(ClientResponse.class, Neo4jQueryJSON.toJSONString());  
           String result = response.getEntity(String.class);               
           response.close();
           int status = response.getStatus();      

           System.out.printf("POST %s %nstatus code [%d] %nresult %s%n", Neo4jQueryJSON, status, result);
 } 

我试图检查节点是否存在,if-not,然后只创建一个以及它们之间的关系。

String query = "CREATE (n:color {color: {name} }), (m:value {value: {name} }), (n)-[:hasVALUE]->(m)";

1 个答案:

答案 0 :(得分:1)

这很简单,我找到了基于 SO 答案的解决方案:Check whether a node exists, if not create

   String query = "MERGE (n:color {color: {name} })MERGE (m:value {value: {name} }) MERGE (n)-[:hasVALUE]->(m)";