On Mon, Sep 18, 2017 at 4:41 PM, Dave Clissold <[email protected]>
wrote:

> Hi Michael
>
> I have submitted a gist for approval, interestingly whilst writing it I
> found a better solution to the problem I was facing, would be great to get
> your thoughts on it and if there might be a tidier solution.
>
> http://portal.graphgist.org/graph_gists/matching-against-
> multiple-nodes-with-restrictions-based-on-relationship-parameters
>
> Am I correct in thinking at the end of this, the doubled value in the
> sum(c3.amount) is because of bi-directional relationships?
>

Yes they will be matched twice.

This should be the most efficient one:

Drive the 2nd match from the ingredient with the fewest products and then
for each of those products check the total count and the individual
ingredients.

MATCH (:Product {name: "Product2"})-[c:CONTAINS]->(i:Ingredient)
WITH i ORDER BY size( (i)<-[:CONTAINS]-() ) ASC
WITH collect(i) AS ingredients
WITH head(ingredients) as first, tail(ingredients) as rest
MATCH (p)-[:CONTAINS]->(first) WHERE size( (p)-[:CONTAINS]->() ) =
size(rest) +1 AND all (i in rest WHERE (p)-[:CONTAINS]->(i) )
RETURN p



>
> On Sunday, 17 September 2017 22:03:54 UTC+1, Michael Hunger wrote:
>>
>> Dave, do you think you could set up e.g. a graphgist with some sample
>> data and the different queries you've tried?
>>
>> That would make it much easier to help and try out alternative queries
>>
>> Micahel
>>
>> On Sun, Sep 17, 2017 at 12:39 AM, Dave Clissold <[email protected]>
>> wrote:
>>
>>> Ok have found the error with the first query
>>>
>>> MATCH (p1:Product {name: "P1"})-[c:CONTAINS]-(i:Ingredient) WITH collect
>>> (i.name) as i1 MATCH (p2:Product)-[:CONTAINS]-(i2) WITH p2, collect(i2.
>>> name) as matched, i1 WHERE ALL(x in i1 WHERE x in matched) return p2
>>>
>>> I was not declaring the parameter name in the collect i2
>>>
>>> Now to try and work out if there is a way of completing the 2nd query,
>>> is a FOREACH loop likely to work here?
>>>
>>>
>>> On Thursday, 14 September 2017 12:31:42 UTC+1, Dave Clissold wrote:
>>>>
>>>> Hi All
>>>>
>>>>
>>>> I am trying to match a product against multiple ingredients, the
>>>> returned node must contain all of the ingredients, sometimes there is a
>>>> quantity of how much of an ingredient is used. The number of ingredients
>>>> can change, so using an array is preferred, but this is part of an API
>>>> project being built in javascript so we can use FOR IN loops etc if it
>>>> makes things easier.  As an example I've set up this in the console
>>>> http://console.neo4j.org/?id=39ead
>>>>
>>>>
>>>> I have 2 ways of querying that I cannot solve
>>>>
>>>>
>>>> *1.*
>>>>
>>>>
>>>>  When running a WHERE IN query, like this one below
>>>>
>>>>
>>>> MATCH (p:Product)-[:CONTAINS]-(i:Ingredient) WHERE i.name IN['D','E',
>>>> 'F'] RETURN p
>>>>
>>>>
>>>> It will treat the IN statement as an OR match against D, E or F and
>>>> return all :Products that contains these, in example (P1, P3, P4), is
>>>> there a way to declare this as AND, so the match is against D, E and F, so
>>>> that it only returns products that Contain all of these (P3, P4). I had
>>>> found this post on StackOverflow, https://stackov
>>>> erflow.com/questions/25939493/neo4j-match-multiple-destination-nodes which
>>>> lead me to trying this query but it returns no rows
>>>>
>>>> MATCH (p1:Product {name: "P3"})-[c:CONTAINS]-(i:Ingredient) WITH
>>>> collect(i.name) as i1, c MATCH (b:Product)-[:CONTAINS]-(i2:Ingredients)
>>>> WITH b, i1, i2, c, collect(i2) as matched2 WHERE ALL(x IN i1 WHERE x in
>>>> matched2) RETURN b
>>>>
>>>>
>>>> *2.*
>>>>
>>>>
>>>> When there is a value of amount is there a way to run a similar query,
>>>> but also match a parameter from the relationship as below,
>>>>
>>>>
>>>> MATCH (p1:Product {name: "p2"})-[c1:CONTAINS]-(i1:Ingredient)
>>>> WITH c1 - 5 AS amountLow, c1 + 5 as amountHigh, i1
>>>> MATCH (p2:Product)-[c2:CONTAINS]-(i2:Ingredient) WHERE i2.name = i1
>>>> AND amountLow < c2.amount < amountHigh
>>>>
>>>>
>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Neo4j" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Neo4j" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to