0

I am using AGE to create a graph database, and I want to ensure that all nodes in my database are unique. I want to avoid having multiple nodes with the same properties and labels in my graph.

For example, if I have a node with label Person and properties name and age, I want to ensure that there is only one node with the label Person and the same name and age properties. If a second node is created with the same label and properties, I want to prevent it from being added to the database, or find a way to merge the two nodes into one.

cybersam
  • 63,203
  • 6
  • 53
  • 76
Abdul Manan
  • 117
  • 5

10 Answers10

0

You can write a constraint to prevent any duplicates in your graph database like:

CREATE CONSTRAINT ON (p:Person)
ASSERT (p.name, p.age) IS NODE KEY

Further, you can also use a MERGE statement which creates a new node if it does not already exists or updates the properties of the existing node.

han
  • 74
  • 7
0

One way to do this is to define the UNIQUE INDEX on the properties. You can find the way to do this here. But another way to do is to use MERGE clause as follows

SELECT * FROM cypher('test', $$
MERGE (n: Label {name: 'xyz', age: 10})
RETURN n
$$) AS (result agtype);

Reference: Doc

Zainab Saad
  • 728
  • 1
  • 2
  • 8
0

You can achieve this with the MERGE Clause

Usage:

SELECT * FROM cypher('graph_name', $$
MERGE (charlie:label {name: 'Charlie Sheen', age: 10})
RETURN charlie
$$) as (v agtype);

Here this matches the node or vertex with the label and properties… returns it if it already exists or creates it if it doesn’t…

NOTE: This will only work if you don’t already have duplicates in your database as it doesn’t put a constraint on the actual graph. The merge clause only implements a search or create functionality.

Peter
  • 43
  • 4
0

To ensure that all nodes in the database are unique, you can follow an approach of defining a Unique Identifier.

A unique identifier can be added to every node, this property can be helpful to uniquely identify each node. It can be a combination of properties to form a key or any unique identifier UID. So, before a node is added, you can check that node with the same identifier exists or not on the basis of which you can handle it accordingly.

[apache-age] [PostgreSQL]

0

In order to solve this problem, there are two solutions discussed below:

  • Insert only unique nodes: We can use MERGE statement to make sure that only unique nodes are inserted. For example, you want to create a node with name Ali and age 25. Use can do this as:

    MERGE (p:Person {name:'Ali', age: 25})

  • Merge the existing nodes: In this solution, we use a few commands with MERGE statement to update the data of the previous node with that of the new node i.e. merging of two duplicate nodes. If any node same like an existing nodes comes, it will merge with it.

    MERGE (p:Person {name:'Ali', age: 25})

    ON CREATE SET p.property = value

    ON MATCH SET p.property = value

adil shahid
  • 125
  • 4
0

You can add a constraint to check the duplication:

CREATE CONSTRAINT ON (p:Person)
ASSERT (p.name, p.age) IS NODE KEY

and instead of match class, you can use merge class.

Aadil Bashir
  • 111
  • 5
0

I think you should check every node before inserting a new one like creating an index on every node. It might decrease performance so keep certain parameters in mind.

OR

You can use the MERGE clause.

0

You can define a unique index on specific properties e.g. name and age, to prevent duplicate nodes. This ensures that if a node with the same properties is added then it will result in an error or it can be merged into the existing node.

0

By CREATE CONSTRAINT clause, you can create a constraint on the properties of the nodes to ensure that they are unique.

CREATE CONSTRAINT ON (p:pin) ASSERT p.pin IS UNIQUE
0

SELECT * FROM cypher('test', $$ MERGE (n: Label {name: 'xyz', age: 10}) RETURN n $$) AS (result agtype);

UNIQUE INDEX on the properties