0

I am trying to create nodes and relationships between them. Below is the code which I have used.

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);
 } 

I am trying to check is the node exists, if-not, only then create one aswell as the relationship between them.

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

1 Answer 1

1

It was pretty simple, I found the solution based on this SO answer: Check whether a node exists, if not create

   String query = "MERGE (n:color {color: {name} })MERGE (m:value {value: {name} }) MERGE (n)-[:hasVALUE]->(m)";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.