Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What's Cooking? Part 2: What Can I Make With These Ingredients?

DZone 's Guide to

What's Cooking? Part 2: What Can I Make With These Ingredients?

This tutorial explains how to import the BBC Good Food dataset into Neo4j.

· Database Zone ·
Free Resource

My colleague, Lju, started off a series of posts showing how to import the BBC Good Food 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 chilis and want to find some recipes that will make use of them.

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

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

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

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, chilis 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: "chili"})
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:

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:

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

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

Stay tuned for part three, and let us know your thoughts in the comments!

Topics:
database ,tutorial ,neo4j ,import a dataset

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}