Export a (sub)graph to Cypher script and import it again

Oftentimes you want to export a full (or partial) database to a file and import it again without copying the actual database files. If you want to do the latter, use neo4j-admin dump/load. Here are two ways on how to… Read more →

Explore:  


How to avoid costly traversals with join hints

When matching a pattern using Cypher, the number of possible paths to evaluate often correlates with query execution time. When there is a supernode in the path (a node with a high number of relationships whose type are included in… Read more →

Explore:  


Importing CSV Files: Neo4j Desktop and Sandbox

Loading various kinds of files into Neo4j requires different locations depending on the tool you are using. Import methods we will cover: 1. Neo4j Desktop 2. Neo4j Sandbox Neo4j Desktop Filepath for Neo4j Desktop will vary based on your operating… Read more →

Explore:  


Understanding transaction and lock timeouts

One way to handle runaway queries is to impose a time limit that will terminate a query when exceeded. There are some subtleties here that need to be understood to ensure proper behavior and avoid confusion. Defining a transaction timeout… Read more →

Explore:  


Comparing relationship properties within a path

You want to compare relationship-properties of a path, either by a global value or parameter, or with each other within a path. Basic model: (:Person {person_id:’123′})-[:FRIEND {date:’1972-01-01′}]->(m:Person {person_id:’456′}) Make sure to have an constraint OR index on your nodes: Constraint… Read more →

Explore:  


Limiting MATCH results per row

Since LIMIT applies to the total number of rows of the query, it can’t be used in cases when matching from multiple nodes where the limit must be on match results per row. Take an example case using the Movies… Read more →

Explore:  


Understanding non-existent properties and working with nulls

In Neo4j, since there is no table schema or equivalent to restrict possible properties, non-existence and null are equivalent for node and relationship properties. That is, there really is no such thing as a property with a null value; null… Read more →

Explore:  


Understanding aggregations on zero rows

Aggregations in Cypher can be tricky in some cases. Notably, when performing aggregation right after a MATCH where there are no matches, or after a filter operation that filters out all results. In some cases a query using aggregations in… Read more →

Explore:  


Explanation of error “Unrecognized transaction id. Transaction may have timed out and been rolled back”

When submitting a request via the Neo4j Transactional Cypher HTTP endpoint, one may encounter the following error Unrecognized transaction id. Transaction may have timed out and been rolled back This error may occur as a result of the transactions expiration… Read more →

Explore:  


How do I pass parameters when calling apoc.cypher.runFile

APOC allows one to have a stored procedure, apoc.cypher.runFile, to then run the contents of the file to the Cypher engine. To allow the reading of the file in the conf/neo4j.conf one needs to define apoc.import.file.enabled=true and then if one… Read more →

Explore:  


How do I improve the performance of counting number of relationships on a node

Using Cypher one could count number of relationships in the following manner MATCH (n:Actor {name:’Sylvester Stallone’})–>() RETURN count(*); Which will report the number of incoming/outgoing relationships for the Actor named Sylvester Stallone. Using bin/neo4j-shell and running a profile on the… Read more →

Explore:  


Alternatives to UNION queries

While UNIONs can be useful for certain cases, they can often be avoided completely with small changes to the query. In this article we’ll present various example cases where a UNION isn’t necessary, and a simple Cypher query will do.… Read more →

Explore:  


How do I use LOAD CSV to update/set properties of existing nodes

One can use LOAD CSV to perform a bulk update to existing nodes, and create new nodes, as follows. If we have a .csv called Movies.csv and its content is: code,wysiwyg-indent3 code,wysiwyg-indent3 101,The Matrix,463420706 102,The Matrix Reloaded,738576929 103,The Matrix Revolutions,427289109 104,A… Read more →

Explore:  


Post-UNION processing

Cypher does not allow further processing of UNION or UNION ALL results, since RETURN is required in all queries of the union. Here are some workarounds. Post-UNION processing in Neo4j 4.0 With Neo4j 4.0, post-UNION processing is now possible via… Read more →

Explore:  


Using Cypher to generate Cypher statements to recreate indexes and constraints

The following can be used to extract index definitions and constraint definitions from an existing database and the resultant output can be played back on another Neo4j database. For example with the :play movies dataset as when run from the… Read more →

Explore:  


How to generate sysinfo output from Cypher

If you need to generate the equivalent output from command :sysinfo as run from the Neo4j Browser at http://localhost:7474 this can be achieved by running the following Cypher call dbms.queryJmx(“org.neo4j:instance=kernel#0,name=Store file sizes”) yield attributes with keys(attributes) as k , attributes… Read more →

Explore:  


All shortest paths between a set of nodes

Consider a number of arbitrary nodes, A,B,C,D,E,F,…​.. I wish to return all of the shortest paths between these nodes. The nodes may have many edges between them, but anticipate a maximum of 4. The graph is complex and non hierarchical… Read more →

Explore:  


Performing match intersection

Match intersection is a common use case where you’re searching for nodes which have relationships to all of a set of input nodes. For the rest of the article we’ll use the built-in movies graph for demonstration. The example use… Read more →

Explore:  


Explanation of error “Cannot merge node using null property value for”

When running a MERGE, which is a combination of MATCH and/or CREATE one may encounter an error of Cannot merge node using null property value for if the MERGE is performing a MATCH against a null property. For example, when… Read more →

Explore:  


How do I export Cypher Favorites recorded in the browser

Cypher Favorites are common Cypher statements which one can save to the left panel of the Neo4j browser. A Favorite is created by entering the Cypher at the top prompt and then clicking the Favorite icon to the right as… Read more →

Explore:  


Query to kill transactions that take longer than X seconds and doesn’t contain certain keywords

In Neo4j we currently have the configuration property referred to as execution guard: dbms.transaction.timeout=30s that can be set automatically to kill transactions that take more than “x” seconds (x is equal to what is assigned to dbms.transaction.timeout, in this case… Read more →

Explore:  


How do I display the nodes with the most properties

To display the nodes with the most properties defined, run the following Cypher: MATCH (n) RETURN labels(n), keys(n), size(keys(n)), count(*) ORDER BY size(keys(n)) DESC Representative output is similar to: labels(n) keys(n) size(keys(n)) count(*) [Movie] [TotalRevenue, year_of_release, name, id] 4 1… Read more →

Explore:  


Updating a node but returning its state from before the update

Some use cases require updating node (or relationship) properties, but returning the node (or relationship) as it was prior to the update. You’ll need to get a ‘snapshot’ of the node before the update, and return that snapshot instead of… Read more →

Explore:  


Performing pattern negation to multiple nodes

Some use cases require matching to nodes which aren’t connected to any of some other set of nodes. We’ll discuss both incorrect and correct approaches to this kind of query. For our examples we’ll use a recipe graph, where the… Read more →

Explore:  


Tuning Cypher queries by understanding cardinality

Cardinality issues are the most frequent culprit in slow or incorrect Cypher queries. Because of this, understanding cardinality, and using this understanding to manage cardinality issues, is a critical component in Cypher query tuning, and query correctness in general. A… Read more →

Explore: