SingleShot
SingleShot

Reputation: 19131

Neo4J Cypher Query to Match Collections Against Other Collections

I'm new to Neo4J and really struggling on a query. This is not my actual domain, but I'm trying to simplify it down to a similar problem.

Given:

I want:

Conceptually a way to solve this might be:

  1. create a combined list of all Fruits provided by Markets in the Region
  2. create a combined list of all Vegetables provided by Markets in Region
  3. for each Recipe, check that its required Fruits are in the Fruit list
  4. for each Recipe, check that its required Vegetables are in the Vegetable list
  5. all Recipes that pass these checks are returned

Of course, a Cypher query will likely look very different from this. Any advice offered is greatly appreciated.

Upvotes: 1

Views: 30

Answers (1)

stdob--
stdob--

Reputation: 29172

Suppose that there is the following data scheme:

enter image description here

Then the query looks like this:

  • for each potential recipe from the region, calculate the number of products that this region provides for this recipe
  • for each potential recipe, calculate the number of required products
  • compare

MATCH (R:Region {name: 'My Region'})-[:contains]->(M:Market)
                                    -[:provides]->(P:Product)
                                    <-[:requires]-(RC:Recipe)
WITH R, RC, 
        count(DISTINCT P) AS productsCountForRecipeByRegion
MATCH (RC)-[:requires]->(P:Product)
WITH R, RC, productsCountForRecipeByRegion, 
     count(P) as productsCountForRecipe 
     WHERE productsCountForRecipeByRegion = productsCountForRecipe
RETURN R, RC

Upvotes: 1

Related Questions