anyweez
anyweez

Reputation: 941

Neo4j: match against multiple end nodes

I have a neo4j database that contains a bunch of ingredient and recipe nodes with CONTAINS relationships between them, i.e. (recipe)-[CONTAINS]->(ingredient).

I want to find all recipes that contain two specific ingredients, but I can't figure out how to do it. Conceptually it seems like what I want is:

START ingr1=node:Ingredients(id=1), ingr2=node:Ingredients(id=2)
MATCH recipe-[CONTAINS]->ingr1,
      recipe-[CONTAINS]->ingr2
RETURN recipe

Apparently it's not OK to match against the same relationship with different start and end nodes. What's the right way to think about this, and what would a proper query look like?

Upvotes: 0

Views: 347

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39905

Your query is almost perfect, you're just missing a colon in front of the relationship type:

START ingr1=node:Ingredients(id=1), ingr2=node:Ingredients(id=2) 
MATCH recipe-[:CONTAINS]->ingr1,
      recipe-[:CONTAINS]->ingr2
RETURN recipe

Without the colon, CONTAINS is a variable name for the relationship. Obviously the same relationship cannot have two different end nodes at the same time, that's why you're getting an empty result.

Upvotes: 2

Related Questions