DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. What's Cooking? Part 2: What Can I Make With These Ingredients?

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.

Mark Needham user avatar by
Mark Needham
·
Mar. 04, 19 · Tutorial
Like (2)
Save
Tweet
Share
4.13K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Database

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OpenVPN With Radius and Multi-Factor Authentication
  • Apache Kafka Is NOT Real Real-Time Data Streaming!
  • Public Key and Private Key Pairs: Know the Technical Difference
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: