0
votes

first of all English is not my first language so I was not entirely sure how to tell you what I need. I hope this description makes it better.

I have a graph in neo4j containing two types of labels: cocktails and ingredients. they are connected by a relation called: CONTAIN.

Example:

(:cocktail{name:"gin tonic"})-[:CONTAINS]-(:ingredient{name:"gin"} (:cocktail{name:"gin tonic"})-[:CONTAINS]-(:ingredient{name:"tonic water"}

Now my question is: I want to input a list of ingredients and output all cocktails possible to make with this list. The problem is: how do I get all those cocktails, even the ones that do not contain ALL of the ingredients provided in the list?

Example input:

"gin","tonic water","vodka"

Example output:

"tonic water"<-"gin tonic"->"gin"

thanks in advance!

1

1 Answers

0
votes

First you need to go through the list of ingredients and get a list of all possible cocktails. And then choose only those cocktails, all the ingredients of which are in the list of ingredients:

WITH ["gin","tonic water","vodka", "lemon"] AS ingredientNames

UNWIND ingredientNames AS ingredientName
MATCH (c:cocktail)-[:CONTAINS]-(i:ingredient {name: ingredientName})

WITH c, 
     collect(DISTINCT i) AS ingredients
MATCH (c)-[:CONTAINS]->(i:ingredient)

WITH c, ingredients,
     collect(DISTINCT i) AS ingredientsTest
     WHERE SIZE(ingredients) = SIZE(ingredientsTest)

RETURN c AS cocktail,
       ingredients