Posts

What is a Node

A Node is the 'Table' of the Graph Database world.   The main entity in a graph. They are also sometimes referred to as vertices or points. In our ASCII art world, a pair of parentheses is used to represent nodes. This resembles a circle and provides a simplified syntax for representing a node in ASCII:     ()     (node) The variable (node) holds node values so they can be processed or returned in a query later on. If you do not need to do anything with the node, you can skip the use of the variable. 

MERGE made a new Node

MERGE is a powerful and misunderstood component of the CYPHER toolbox. Assume you have two :Person nodes each with a name property ('Adam' and 'Bob'). You might assume that you could MERGE as follows to create a [:KNOWS] relationship: MERGE (a:Person {name:'Adam'})-[r:KNOWS]->(b:Person {name:'Bob'}); and you'd be correct, it works perfectly Always remember MERGE is based on matching the entire pattern in the statement. Say you now want to set the age of Adam to 21 in the same statement. You might think it's as simple as MERGE (a:Person {name:'Adam',age:21})-[r:KNOWS]->(b:Person {name:'Bob'}); will create 2 new Person nodes and a [:KNOWS] relationship. If there is a unique constraint on :Person (name) there will be errors thrown and no new nodes or relationships. Why, the initial part of the MERGE 'looks' for a pattern match on the :Person node of name:'Adam' and age:21, it doesn't create a property. No m...

Delete Nodes Without Labels.

Delete nodes without labels MATCH (n) WHERE size(labels(n)) = 0 DETACH DELETE n Similarly you can find nodes without labels using: MATCH (n) WHERE size(labels(n)) = 0 RETURN n

Removing Duplicate Relationships

From Neo4j v3.5 and onwards you can use a generic CYPHER statement: match (s)-[r]->(e) with s,e,type(r) as typ, tail(collect(r)) as coll foreach(x in coll | delete x) to remove all duplicate relationships in your Neo4j Graph Database. You can refine the control over what is deleted by adding in a node label or relationship type. We're collecting the relationships and grouping by type (as well as start and end node), so if any collection is greater than 1, then there are multiple relationships with the same type. TAIL removes the first element from the collect statement, but the first relationship back into rows then delete them. If you don't have labels to use for your query, then the query will be graph-wide, which is likely to take much longer to execute, and may encounter issues if the set of relationships to delete is too large to handle all at once.

Visualising Your Database

To show a visualisation of the Nodes and Relationships in your Neo4j instance, run the following in your Neo4j Browser: CALL db.schema.visualisation()

Count your Nodes and Relationships

 How many Nodes and relationships are in your Graph? You could look on the left hand side of the Neo4j Browser, but that would be cheating. For nodes: MATCH (n) RETURN count(n) For Relationships: MATCH ()-[r]->() RETURN count(r) The above CYPHER statements assume all nodes and relationships are to be counted. Once you start adding in labels and relationships you will filter the return values For Outgoing Relationships: MATCH (a)-[r]->() RETURN count(a) This counts nodes with outgoing relationships only.

New blank Neo4j Database

Recipe for creating new ‘blank’ database. Putty onto the server  cd /etc/neo4j sudo vi neo4j.template Uncomment and edit and change one line – the name of the database #***************************************************************** # Neo4j Community configuration # # For more details and a complete list of settings, please see # https://neo4j.com/docs/operations-manual/current/reference/configuration-settings/ #***************************************************************** # The name of the database to mount dbms.active_database=prod.db cypher.forbid_shortestpath_common_nodes=false # Paths of directories in the installation. dbms.directories.data=/var/lib/neo4j/data restart the machine / service. When you now check the dbms.directories.data directory there is another directory alongside the default graph.db The browser now shows an empty database with no nodes, relationships or properties.