Posts

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.

Determining Node Label 'Names'

Determining Node labels From a post on the Neo4j blog we have a path to finding our generic node name (x) How do I display all nodes with no defined labels Although assigning a node one or more labels provides many benefits (i.e. performance gains from index usage, ability to group nodes into sets, etc), it is possible to create a node without any labels. The following Cypher can be used to identify all nodes in the graph which have no label defined: MATCH (n) WHERE SIZE(labels(n)) = 0 RETURN n It points the way for us to do some generic style Cypher (and solve for (x) ). What is node label (x)? Assuming we have a network of nodes (c) and we are tasked with applying common metadata against these nodes with fixed values and groupings it makes sense to utilise the strengths of the graph engine to 'TAG' the nodes in the network represented by (c). If we create several 'tag' nodes and our sample node (c); for example: CREATE (x:UPWARD  {name:'upward'} ) CREATE (x:DO...

Property DataType - apoc.meta.type

Node Properties in Neo4j. A property can be of any datatype even within nodes of the same label. Nodes have no fixed schema as with the RDBMS where most notions of data storage are formed and 'best practice' is enforced.  Properties themselves do not have types, but the values they hold do. However, you can have a property, X which could be assigned a string value or a numeric value. There is no enforcement of typing for a property. Of course, it would not (always) make sense to assign a property different type values in your graph. So, just what datatype is the property? Trusty APOC has a function for that. WITH [true, 42, 'foo', 1.2] AS data UNWIND data as value RETURN apoc.meta.type(value) So, just what datatype is the property? "BOOLEAN" "INTEGER" "STRING" "FLOAT" You can use this function in MATCHES on nodes in your system where properties are unknown at runtime. MATCH (n:MyNode) RETURN apoc.meta.type(n.MyProperty)

Mass removal of Edges and Nodes - DELETE

Deleting of Nodes:  To delete a node it should have no relationships Delete all nodes and relationships match (n)-[r]-() delete n, r Delete all nodes which have no relationships match (n) delete n Delete only ExampleNodes which have no relationships match (n:ExampleNode) delete n Delete all relationships match (n)-[r]-() delete r Delete only ExampleRelation relationships match (n)-[r:ExampleRelation]-() delete r