In Cypher, how can I create a relationship if it doesn't exist; update property if it does

neo4j no relationshipstate for added relationship
neo4j create node if not exists
neo4j undirected relationship
neo4j add relationship to existing nodes
neo4j merge multiple relationships
on create set neo4j
create unique is no longer supported please use merge instead
cypher upsert

In Cypher in Neo4J, given two nodes, if there's no relationship between them, I'd like to create a relationship (of type Foo) with a weight property of one. If this relationship already exists, I'd like to increment its weight property.

Is there a good way to do this in a single Cypher query? Thanks!

Edit: Some additional details: The nodes are already created, unique, and in an index.

This is exactly why we added CREATE UNIQUE in 1.8.

START a=node(...), b=node(...)
CREATE UNIQUE a-[r:CONNECTED_TO]-b
SET r.weight = coalesce(r.weight?, 0) + 1

Read more about CREATE UNIQUE here, the question mark here, and coalesce here.

Create relationship, if another relationship doesnt exist, Hi I have a relationship between a vessel and a product (vessel)-[:HasInventory]- >(product) when I forecast what a vessel may need to� To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them. Query. MATCH (a:Person),(b:Person) WHERE a.name = 'A' AND b.name = 'B' CREATE (a)-[r:RELTYPE]->(b) RETURN type(r) The created relationship is returned by the query.

To complete Andres answer, question mark at the end of a property is now an error with Neo4j 2. So request will be :

MATCH a, b
WHERE a(...) AND b(...)
CREATE UNIQUE a-[r:CONNECTED_TO]->b
SET r.weight = coalesce(r.weight, 0) + 1

Create relationship/properties on existing nodes - Cypher, Hello folks, I imported Log Data in Neo4j from Elastic Search and the next step would be the connection of the (raw) Is there a hint to create the relationship ? network_DIRECTION}] it would fail, since e doesn't exist. Create an Index ; Just like creating nodes in Neo4j, we can use the CREATE statement to create relationships between those nodes. The statement for creating a relationship consists of CREATE, followed by the details of the relationship that you're creating. Example. Let's create a relationship between some of the nodes that we created

For future reference, CREATE UNIQUE has since been deprecated (see here). It looks like you can do something similar with MATCH and MERGE:

                MATCH (a:Person {name: 'Wonder Woman'})
                MERGE (b:Person {name: 'Aries'})
                MERGE (a)-[r:FOUGHT]->(b)
                ON CREATE SET r.weight = 1
                ON MATCH SET r.weight = r.weight + 1

So here, Wonder Woman fought Aries at least once, else it will increment the weight.

Relationship Direction in Cypher is Important, Relationship direction between two nodes is required for Cypher write by a larger group as OpenCypher to make Cypher the SQL for graph databases. Querying data in the graph may seem flexible in that Cypher doesn't� To create nodes and relationships using Cypher, use the CREATE statement. The statement consists of CREATE, followed by the details of the node or relationship that you're creating. Example. Let's create a music database that contains band names and their albums. The first band will be called Strapping Young Lad. So we will create an Artist

Relationship property doesn't get created � Issue #19 � neo4j-contrib , What I'm trying to accomplish here, is to create a relationship between two given nodes. This relationship should contain a property "nearness" that represents� Creating Relationships. We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax. Syntax. Following is the syntax to create a relationship using

Querying — Neo4j.rb 7.2.3 documentation, This allows for the method chaining to build queries, but it also enables eager loading of If that doesn't work, it creates the node, then creates the relationship. It did the Requires relationship all the way through the hierarchy until it didn’t have anywhere to go. This isn’t a great way to create a dependency, so let’s explore a better dependency with the Battlecruiser as an example. Query 9: What Are All The Buildings Required to Construct a Battlecruiser? Cypher’s Shortest Path Function

Neo4J and Virtual Nodes/Relationships, This article takes a look at Neo4j and virtual nodes and relationships. It's often the case that the database schema used for loading data doesn't translate well To do this, we can create virtual nodes for the registrants and� You can put the exact relationship type in the MATCH pattern, but sometimes you want to be able to do more advanced filtering on the type. You can use the special property type to compare the type with something else. In this example, the query does a regular expression comparison with the name of the relationship type.

Comments
  • Are you okay with using a SNAPSHOT version of neo4j? In neo4j 1.8 you can update the graph using Cypher, but not in any other versions.
  • So essentially you are looking to scan all nodes and if connected, increment, if not, create relationship?
  • Nicholas, thanks for response. I added some additional details; the nodes are unique and indexed, so I don't think I should have to scan all the nodes. But, other than that, your comment is correct: if connected, increment, if not, create relationship.
  • Perfect! Just what I was looking for. I knew about RELATE, but not the coalesce phrase. What exactly does the "?" do?
  • Since Neo4j 2.0 the ? operator has been removed. See: neo4j.com/docs/snapshot/…
  • Create unique is now deprecated, use MERGE instead: neo4j.com/docs/developer-manual/current/cypher/clauses/merge