I'm trying to make dynamically building cypher query with python. I have a list of dicts, that contains pair of Node name and Node value. I need to filter Node:Goods by related Node:Property. Let's say that one Node:Goods has many Node:Property, and I need to filter :Goods by :Property name and value. For example I need all :Goods that has a relation with :Property {name: "Color", value: "blue"}
and another :Property {name: "Material", value: "Oak"}
. For that case I figure out query like this:
MATCH (g:Goods) WHERE EXISTS( (g)-[:PROP]-(:Property {name: "Color", value: "blue"})) AND EXISTS( (g)-[:PROP]-(:Property {name: "Material", value: "Oak"})) RETURN g
Python gets list of dicts and for each dict makes string EXISTS( (g)-[:PROP]-(:Property {name: %s, value: %s}))
. It works perfectly, but what if I need to make compare between some properties? For example - price range? I cant assign a variable for Node:Property inside EXISTS()
, also I cant use WHERE
inside other WHERE
statement. How can I query all :Goods with :Property {name: "Color", value: "blue"}
and another :Property {name: "Price", value: "> 190, <320"}
? There is no problem to pass to query any arguments or build it dynamically from list of dicts, there is problem with my Cypher knowledge :). Please help!
For this case, you would be best served with a MATCH where you could use a WHERE for the range:
...
WITH g
MATCH (g)-[:PROP]-(p:Price)
WHERE 190 < p.value < 320
...
If that won't work for the way you're assembling the query, you could instead use a pattern comprehension, which will provide a list based upon a MATCHed pattern and WHERE clause:
...
WITH g, size([(g)-[:PROP]-(p:Price) WHERE 190 < p.value < 320 | p]) > 0 as priceInRange
WHERE priceInRange
...
(you should of course parameterize the upper and lower bounds of the price range)
If the pattern comprehension yields a non-empty list then the price is in range.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.