How to query multiple property values out of order

2019-09-21 00:39发布

问题:

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


标签: neo4j cypher