Articles tagged as cypher
A method to calculate the size of an index in Neo4j.
If the need arises to calculate the size of an index in Neo4j, for capacity planning purposes, there are two methods available: 1) Execute the db.indexes() procedure: CALL db.indexes() YIELD…
A note on OPTIONAL MATCHes
An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for…
Achieving longestPath Using Cypher
While Cypher is optimized for finding the shortest path between two nodes, with such functionality as shortestPath(), it does not have the same sort of function for longest path. In…
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…
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…
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: Make sure to have an constraint…
Conditional Cypher Execution
At some point you’re going to write a Cypher query requiring some conditional logic, where you want different Cypher statements executed depending on the case. At this point in time…
Cross Product Cypher queries will not perform well
Just like SQL, if you do not properly connect the parts of your query, it will result in a cross (cartesian) product, which is seldom what you want. Take the…
Importing Data to Neo4j in the Cloud
Loading data in a Neo4j instance that is in the cloud is very similar to running Neo4j using any other method. However, there are a few small things to look…
Explanation of the "consumed after" message in query results
After successfully executing a query through the Neo4j Browser or cypher-shell, you may see a message formatted as follows accompanying the query results: This provides the following information: These are…
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…
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 This error may occur as a result of the transactions expiration date/time being…
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,…
Fast counts using the count store
Neo4j maintains a transactional count store for holding count metadata for a number of things. The count store is used to inform the query planner so it can make educated…
Geocoding with Arcgis
Prerequisites Create/obtain an Arcgis account. Create application within your account. The application will be assigned a 'client_id' and 'secret'. APOC The APOC library provides a apoc.spatial.geocode('address') procedure (as well as…
How do I convert a property representing a date timestamp to another timezone
Temporal datatype support was introduced with with Neo4j 3.4 and as a result it is possible to record a date timestamp with timezone as a property value. The following Cypher…
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…
How do I display the nodes with the most properties
To display the nodes with the most properties defined, run the following Cypher: Representative output is similar to: The first row of output indicates that there is a Label named…
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…
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 Which will report the number of incoming/outgoing relationships for the Actor named Sylvester Stallone. Using bin/neo4j-shell and running…
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…
How do I produce an inventory of statistics on nodes, relationships, properties
Using the following Cypher will produce an 'inventory' of the nodes within the graph and statistics related to number of Nodes per label, average number of properties, minimum number of…
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: and…
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…
How to avoid using excessive memory on deletes involving dense nodes
In situations where you know you need to delete a bunch of nodes (and by rule their relationships as well), it can be tempting to simply use DETACH DELETE and…
How to check for time range overlap in Cypher
Neo4j 3.4 introduced temporal types into Cypher, so now we have dates, dateTimes, and their local versions, too, as well as durations. While we don’t have a type for time…
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 To which…
How to implement a primary key property for a label
Commencing with Neo4j 2.3.x it is possible to create the equivalent of a primary key on a property of a label. For example the following Cypher: will create two constraints…
How to write a Cypher query to return the top N results per category
The following Cypher describes how you can display the Top 5 test scores within an entire :Score population broken out by a field_of_study property. running: will return output of: and…
Importing CSV Files: Neo4j Aura, 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: Remote: Neo4j Aura and Neo4j Sandbox Local: Neo4j…
Large Delete Transaction Best Practices in Neo4j
In order to achieve the best performance, and avoid negative effects on the rest of the system, consider these best practices when processing large deletes. Start by identifying which situation…
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…
Neo4j & JDBC: the Neo4j JDBC Driver vs. BI Connector
This page describes the connection between JDBC and Neo4j, and when users should use the BI Connector, vs. when they should use the Neo4j JDBC Driver. To begin with, let’s…
Neo4j: Convert string to date
Neo4j 3.4 saw the introduction of the temporal date type, and while there is now powerful in built functionality, converting strings to dates is still a challenge. If our string…
Pass Temporal Objects as Parameters
With the support of datetime types in Neo4j, users might wonder how or if it works to transport those types along with other data types via the drivers. It is…
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…
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.…
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…
Protecting against Cypher injection
What is Cypher injection? Cypher injection is a way for maliciously formatted input to jump out of its context, and by altering the query itself, hijack the query and perform…
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: that can be set automatically to kill transactions that take more than “x” seconds (x is equal…
Resetting query cardinality
As queries execute, they build up result rows. Cypher executes operations per-row. When a query is made up of completely separate parts, unrelated to each other, and you don’t want…
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…
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…
Understanding how MERGE works
What is MERGE, and how does it work? The MERGE clause ensures that a pattern exists in the graph. Either the entire pattern already exists, or the entire pattern needs…
Understanding Neo4j Query Plan Caching
This article is based on the behavior of Neo4j 2.3.2. Query plan caching is governed by three parameters, as defined in the conf/neo4j.properties file, which are detailed here. The three…
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…
Understanding the Query Plan Cache
When a Cypher statement is first submitted Neo4j will attempt to determine if the query is in the plan cache before planning it. By default Neo4j will keep 1000 query…
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…
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…
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…
Using max() and min() while keeping items
The aggregation functions of max() and min() are very useful, but you can sometimes find yourself fighting against Cypher’s aggregation behavior for cases that should be simple. This often comes…
Access to the neo4j-shell in NEO4J CE 3.x
From Neo4j 3.0 access to neo4j-shell is no longer possible from the desktop-installers for Windows and OSX. To use neo4j-shell, you have to download the TAR/ZIP distribution from: http://neo4j.com/download/other-releases/ For…
Using Subqueries to Control the Scope of Aggregations
Aggregations, such as collect() and count(), show up as EagerAggregation operators (with dark blue headers) in query plans. These are similar to the Eager operator in that it presents a…
Why doesn’t my WHERE clause work?
It can be frustrating when it seems like a WHERE clause isn’t working. You can use these approaches to figure out what’s wrong. Check for WHERE clauses following OPTIONAL MATCH…
Working with streaks in Cypher
When using Cypher for data analysis, you might have a problem where you need to identify or filter based upon some kind of streak. For example, for a sports graph,…