How to query multiple property values out of order in Cypher

大兔子大兔子 提交于 2020-01-07 08:12:13

问题


I have a list of product names [shirt,shoes,pants,hat,glasses] I'm trying to perform a query that will return the products using regular expression that's case insensitive and in any order as long as the value exists.

I'm only able to query them in order MATCH (product:Product) WHERE product.name =~"(?i).*shirt.shoes." RETURN product

How can I query them out of order ie MATCH (product:Product) WHERE product.name=~"(?i).*hat.shirt."?

Thanks


回答1:


What exactly do you want to search for with the regular expression? Are you looking for product names that contain just one of the keywords in your list? Or multiple keywords?

If you just want to find product names that contain at least one of the keywords in your list you could use the string comparison operator CONTAINS along with the toLower() function to make the comparison case insensitive:

WITH {products} AS products
MATCH (p:Product) WHERE any(x IN products WHERE toLower(p.name) CONTAINS toLower(x))
RETURN p

Where {products} is your array of product names: ['shirt', 'shoes', 'pants', 'hats', 'glasses']

Edit

To find Product nodes that contain all keywords, use the all() list predicate.

For example, let's say you want to find all Product nodes where the name contains "shirt" and "shoes":

WITH ["shirt", "shoes"] AS keywords
MATCH (p:Product) WHERE all(x IN keywords WHERE toLower(p.name) CONTAINS toLower(x))
RETURN p


来源:https://stackoverflow.com/questions/38702414/how-to-query-multiple-property-values-out-of-order-in-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!