Get startedGet started for free

Querying a knowledge graph

1. Querying a knowledge graph

Now that we've created and populated our Neo4j graph database, let's take a look at how to read data from and write data to it.

2. Cypher is pattern matching with ASCII-art

Cypher is a query language that allows you to create and read patterns of nodes and relationships in your Neo4j database. An example pattern may be people who have acted in movies. We use the MATCH clause to read data from the database.

3. Cypher is pattern matching with ASCII-art

And drawing the pattern with text characters. Nodes are drawn with parentheses

4. Cypher is pattern matching with ASCII-art

with the information about the node inside. The colon denotes the node on the left should have a label of person, and the node on the right should have a label of movie.

5. Cypher is pattern matching with ASCII-art

Relationships are drawn with two dashes and an arrow to specify the direction where necessary.

6. Cypher is pattern matching with ASCII-art

Information about the relationship is defined inside a box drawn with square brackets. The colon here defines the type of the relationship, as ACTED_IN.

7. Cypher is pattern matching with ASCII-art

Nodes and relationships can be assigned to a variable, in this case "a" refers to the Person node.

8. Filtering results

The WHERE clause can be used to filter results, for example the name property on the Person node.

9. Controlling outputs

The RETURN clause defines the query's output. In this case, the title property of the movie node and the roles property of the ACTED_IN relationship. In the WHERE and RETURN clauses, we can see how the use of variables greatly improves the brevity of the query.

10. How Cypher works

After using labels or an index to find the initial nodes, relationships are followed by type and direction until the entire pattern is found. Unlike relational databases, graph databases do not use any underlying indexes to calculate joins, leading to performance benefits when querying across complex patterns.

11. The CREATE clause

The CREATE statement will create a pattern in the database. A pattern can be as simple as a single node or a complex pattern containing many nodes and relationships. We recommend breaking down your statements into individual parts. This doesn't affect the write performance, but does make your statement easier to read.

12. The MERGE Clause

The MERGE clause is used to find or update a pattern in the graph. If the pattern already exists, it will be returned, otherwise the pattern will be created. Here, for example, we check if a Person node is present with a name property of "Adam". If it is, we get a match, if it isn't, it's created. Likewise, we can check for a Company node with name "Neo4j".

13. Cypher statements in LangChain

To execute a query, use the .query() method on the Neo4jGraph instance. The query method expects two parameters, the Cypher statement as a string and a dictionary of parameters. These are used to avoid Cypher injection, and are prefixed in the query with a dollar sign.

14. Let's practice!

Are you ready to write your first Cypher statement? Let's go!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.