cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

WHERE a Property contains any one of a list of strings

gq16
Node Clone

Hi Neo Friends!

The query below is a modified version of a query I made that works when I am searching in the CONTAINS clause for only a single product type, but I've tried multiple ways of searching for more than one, such as making a list with commas, or using the OR operator, but I can't get it to work. Any advise?

Cypher that is having issues:

 

MATCH (type:ProductType)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
WHERE type.name CONTAINS ("SuperU", "UAN", "Urea")
RETURN type.name AS ProductType, Q.quantity AS Quantity
 
And it doesn't work when I do this either:
''
WHERE type.name CONTAINS ("SuperU" OR "UAN" OR "Urea")
''

 

Works when I do this:

MATCH (type:ProductType)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
WHERE type.name CONTAINS "SuperU" //work with a single string but not multiple
RETURN type.name AS ProductType, Q.quantity AS Quantity
 
Any other advise in optimizing this query would be greatly appreciated as well.
Here is the schema of the nodes of interest:
gq16_0-1654901505752.png

 

 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Try the following, it uses the list predicate 'any' to achieve what you want:

 

WITH ["SuperU", "UAN", "Urea"] as types
MATCH (type:ProductType)
WHERE any (i in types where i = type.name ) 
MATCH (type)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
RETURN type.name AS ProductType, Q.quantity AS Quantity

 

 It would seem that you may have multiple paths for each product name, so do you want to sum the quantity per product name. 

 

WITH ["SuperU", "UAN", "Urea"] as types
MATCH (type:ProductType)
WHERE any (i in types where i = type.name ) 
MATCH (type)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
RETURN type.name AS ProductType, sum(Q.quantity) AS TotalQuantity

 

 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Try the following, it uses the list predicate 'any' to achieve what you want:

 

WITH ["SuperU", "UAN", "Urea"] as types
MATCH (type:ProductType)
WHERE any (i in types where i = type.name ) 
MATCH (type)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
RETURN type.name AS ProductType, Q.quantity AS Quantity

 

 It would seem that you may have multiple paths for each product name, so do you want to sum the quantity per product name. 

 

WITH ["SuperU", "UAN", "Urea"] as types
MATCH (type:ProductType)
WHERE any (i in types where i = type.name ) 
MATCH (type)<-[:HAS_PRODUCT_TYPE]-(:Product)<-[:HAS_PRODUCT]-(:ProductOrigin)<-[:HAS_PRODUCT_ORIGIN]-(c:OrderLine)-[hq:HAS_QUANTITY]->(Q:Quantity)
RETURN type.name AS ProductType, sum(Q.quantity) AS TotalQuantity

 

 

ameyasoft
Graph Maven

Try this:

WHERE type.name CONTAINS ("SuperU") OR type.name CONTAINS ("UAN") OR type.name CONTAINS ("Urea")