What’s cooking? Part 2: What can I make with these ingredients?

Mark Needham
Neo4j Developer Blog
4 min readJan 28, 2019

Last week my colleague Lju started off a series of posts showing how to import the BBC goodfood dataset into Neo4j. Lju also shows how to query the dataset to find recipes written by the same author.

In this post we’re going to continue from where Lju left off and explore the dataset a bit more. Imagine that we have some chillis and want to find some recipes that will make use of those chillis.

We could write the following query, which finds recipes containing chilli, and then returns the name of the recipe along with its list of ingredients:

MATCH (r:Recipe)
WHERE (r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: "chilli"})
RETURN r.name AS recipe,
[(r)-[:CONTAINS_INGREDIENT]->(i) | i.name]
AS ingredients

If we run that query, we’ll see this output:

Recipes containing chilli

I like the look of the beef skewers!

But what about if we want to find recipes that contain more than one ingredient, for example chilli and prawn?

One approach would be to update our WHERE clause to find all recipes that contain those ingredients:

MATCH (r:Recipe)
WHERE (r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: "chilli"})
AND (r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: "prawn"})
RETURN r.name AS recipe,
[(r)-[:CONTAINS_INGREDIENT]->(i) | i.name]
AS ingredients
LIMIT 20

If we run that query we’ll see this output:

Recipes containing chilli and prawns

That works well if we know how many ingredients we want to search for, but not so well if we want to search for a dynamic number of ingredients. To search for a variable number of ingredients we’ll need to use the all predicate function.

Let’s move away from chillis and prawns, and this time find recipes that contain eggs, onions, and milk. We’ll then order the resulting recipes by their number of ingredients in ascending order:

:param ingredients =>   ["egg", "onion", "milk"];MATCH (r:Recipe)WHERE all(i in $ingredients WHERE exists(
(r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: i})))
RETURN r.name AS recipe,
[(r)-[:CONTAINS_INGREDIENT]->(i) | i.name]
AS ingredients
ORDER BY size(ingredients)
LIMIT 20

If we run that query we’ll see this output:

Recipes containing eggs, onions, and milk

Unfortunately I’m allergic to most of the dishes so far, so I want to try and find something that I can eat.

We’ll add a parameter containing allergens to go with our ingredients parameter.

We can then use the none predicate function to make sure that we don’t return any recipes containing the allergens:

:param allergens =>   ["egg", "milk"];
:param ingredients => ["coconut milk", "rice"];
MATCH (r:Recipe)WHERE all(i in $ingredients WHERE exists(
(r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: i})))
AND none(i in $allergens WHERE exists(
(r)-[:CONTAINS_INGREDIENT]->(:Ingredient {name: i})))
RETURN r.name AS recipe,
[(r)-[:CONTAINS_INGREDIENT]->(i) | i.name]
AS ingredients
ORDER BY size(ingredients)
LIMIT 20
Recipes containing coconut milk and rice, but excluding egg and milk

There are still a bunch of tasty looking recipes returned here, even though we’ve taken out two key ingredients.

In this post we’ve learnt how to use three of Cypher’s predicate functions: none(), all(), and exists().

There are two others that we didn’t look at: any(), which can be used to find if a recipe contains any of a collection of ingredients, and single(), which can be used to find recipes that contain exactly one of a collection of ingredients.

For now though, I’ll hand over to Lju for part 3 of the series!

--

--