Knowledge Base

Articles tagged as cypher

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…

Read more

A significant change in apoc.periodic.iterate() in apoc 4.0

In 3.5 an entity (node, relationship, path) could be acquired in one transaction and safely reused by another. However, in 4.0 these entities do hold a reference to their originating…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

Creating and working with linked lists in Cypher

At some point when working with a graph, you may want to create a linked list out of some nodes. If each of the nodes to be linked has its…

Read more

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…

Read more

Using Cypher how do I determine the version and edition of Neo4j

If you want to determine version and edition of the running Neo4j instance this can be accomplished via running the following cypher: The expected output will be:

Read more

Explantion of debug.log message of Commits found after last checkpoint

When running backup for example you may observe in the output of said command detail similar to and see that there is a long pause (i.e. 5+ minutes) from Start…

Read more

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…

Read more

Explanation of error LOAD CSV error of "Couldn’t load the external resource …​"

When running a LOAD CSV Cypher statement, for example whether through bin/neo4j-shell or the browser at http://localhost:7474 this may result in an error as follows And the data/graph.db/messages.log (2.x) or…

Read more

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…

Read more

Fulltext search in Neo4j

Fulltext search in Neo4j is supported by means of fulltext schema indexes. Fulltext schema indexes are created, dropped, and updated transactionally, and are automatically replicated throughout a cluster. For example…

Read more

How do I achieve the equivalent of a SQL Having clause with Cypher

With a traditional SQL based database a HAVING clause will restrict aggregated values. For example will return all zipcodes which have more than 100k residents. To achieve the same in…

Read more

sql

How do I compare two graphs for equality

If you are looking to compare 2 graphs (or sub-graphs) to determine if they are equivalent, the following Cypher will produce a md5sum of the nodes and properties to make…

Read more

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…

Read more

How do I define a LOAD CSV FIELDTERMINATOR in hexidecimal notation

When using LOAD CSV one can define the field delimiter used, whereby the default is the ',' character. If you want to override the default this can be accomplished via…

Read more

How do I define, display, and use parameters with neo4j-shell

bin/neo4j-shell allows for a command line interface to query your graph via Cypher statements and to include parameters to those statements. Usage of parameters, rather than hard coding values, will…

Read more

How do I determine the number of nodes and relationships to be effected by a detach delete

Prior to running a match …​. detach delete n; which will find said nodes and delete all relationships associated with said nodes as well as delete the nodes themselves one…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

How do I perform the equivalent of a SQL Create Table as Select with Cypher

With a traditional SQL RDBMS one could perform a create table as select (i.e. CTAS) whereby its purpose is to create a new table and copy existing data from the…

Read more

How do I produce a profile/explain through cypher-shell and pipeing query file

If yor prepare a file with a Cypher statement that includes either a profile or explain clause and then want to pipe that file to bin/cypher-shell, to produce the profile/explain…

Read more

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…

Read more

How do I report on nodes with multiple labels

If your data model has choosen to define multiple labels on a node, for example To find all nodes which are defined with both labels of Actor AND Director use…

Read more

How do I set a breakpoint in a Cypher statement for further analysis

If you wish to set a 'breakpoint' in a Cypher statement so as to perform further analysis (i.e. see how many locks are taken, memory utilization) one can add a…

Read more

How do I view the column headers of a CSV file with LOAD CSV

If one has a CSV file with the following content and one simply wants to run a LOAD CSV command to have the column headers returned, the following should suffice…

Read more

How does apoc.periodic.iterate work with resources?

How does apoc.periodic.iterate work? For example, when running call apoc.periodic.iterate("MATCH (n) RETURN n", "DETACH DELETE n", {batchSize:1000}) does it append a LIMIT to the MATCH RETURN so that it only…

Read more

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…

Read more

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…

Read more

How to get a high level inventory of objects in your graph (part 2)

Following the knowledge base article on How to get a high level inventory of objects in your graph, this article will cover how to get more detailed high level inventory…

Read more

How to get a high level inventory of objects in your graph

The following Cypher can be used to get a simple high level view of the number of objects within your graph database. This may be used if one is trying…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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.…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

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…

Read more

Using explicit indexes for text search

As of Neo4j 3.4.x, the schema index is optimal for indexing exact property values, but does not support "fuzzy" or full-text search. However, legacy indexing does allow for optimization for…

Read more

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…

Read more

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…

Read more

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,…

Read more