Find all recipes that contain list of ingredients with regex

I use the following cypher to find recipes by ingredients:

WITH ["Salt","Tomatoes","Pepper"] as ingredients
MATCH (r:Recipe)
WHERE all(i in ingredients WHERE exists(
(r)-[:contains]->(:Ingredient {name: i})))
RETURN r.uid AS recipe

And it works great when the ingredients in the array match exactly, but i want it to work when i have an ingredient name with "10g salt" connected to a recipe.

Any idea how i can implement regex or contains into the cypher?

Hey @ralf.boe , you can use CONTAINS with the where clause to modify your query. This is the link from the documentation. (check out section 3.3 from this link)
Also, these links might help you with regex in neo4j:
link1, link2

Hallo @abhishekjayant1111 ,
thanks for your answer, but i really don't know where to put the CONTAINS inside this query. Could you help me?

Hey @ralf.boe, you can try something like this:

WITH ["Salt","Tomatoes","Pepper"] as ingredients
MATCH (r:Recipe)
WHERE all(i CONTAINS 'Salt'  WHERE exists(
(r)-[:contains]->(:Ingredient {name: i})))
RETURN r.uid AS recipe

I'm not sure about the above query, but I think regex might be useful in your case.

If "10g salt" with lower case 's', then try this:

WITH ["Salt","Tomatoes","Pepper"] as ings
unwind ings as i1
match (a:Recipe)-[:CONTAINS]-(b:Ingredient)
where b.name contains toLower(i1)
return a
1 Like

Thanks for your answer @ameyasoft. The cypher doesn't have any error when executed, but i get no results for it.

Post your node with property value = "10g salt". The query worked on my machine and I have a Recipe node with name = "10g salt".

Your Cypher is working, but the weird thing is, that it doesn't match when "Salt" is the first word e.g. "Salt and freshly ground black pepper".

And another thing is that i want to match recipes that contain all the ingredients in the ings list. Any idea how to do it?

Thank you!

First part:
Try this
WITH ["Salt","Tomatoes","Pepper"] as ings
unwind ings as i1
match (a:Recipe)-[:CONTAINS]-(b:Ingredient)
where toLower(b.name) contains toLower(i1)
return a