user697911
user697911

Reputation: 10531

How to express logical OR in property value?

MATCH (s:Product {id:'5001207'})-[r]-> (o) WHERE (o:ProdAttrs) AND any(key in keys(o) WHERE toLower(key) contains 'Network') return o

In this query, if I want to express "contains Network|Phone|Smart", how to modify the query? That is the logic 'OR' in value.

Edited:

I tested this but it doesn't work:

MATCH (s:Product {id:'5001207'})-[r]-> (o) 
  WHERE (o:Attrs OR o:ExAttrs) AND any(key in keys(o) 
   WHERE key =~ '(?!).*(network|phone).*') 
return o

The non-RE version works. Any errors? There is no syntax error. It just doesn't return result as expected from the .

EDIT 2:

My function to generate the query:

public String query_partial_match(String skuid, List<String> attrKey) {

        return "MATCH (s:Product {id:'" + skuid + "'})-[r]-> (o) " +
                "WHERE " +
                "(o:ExAttrs OR o:ProdAttrs) AND " +
                "any(key in keys(o) WHERE key =~'(?i).*(" + attrKey + ").*') " +
                "return o";
    }

This is my Java method to generate the query. So how to use the Java list 'attrKey' in this query?

Upvotes: 0

Views: 68

Answers (2)

InverseFalcon
InverseFalcon

Reputation: 30397

Just use OR in your WHERE clause. Might be helpful to work with the list of all lowercase props.

MATCH (s:Product {id:'5001207'})-[r]->(o:ProdAttrs) 
WHERE any(key in [prop in keys(o) | toLower(prop)] WHERE key contains 'network' OR key contains 'phone' OR key contains 'smart') 
RETURN o

Upvotes: 1

cybersam
cybersam

Reputation: 66999

One option is to use a regular expression to test for multiple alternate string values.

MATCH (s:Product {id:'5001207'})-->(o:ProdAttrs)
WHERE ANY(key in KEYS(o) WHERE key =~ '(?i).*(Network|Phone|Smart).*')
RETURN o;

(?i) enables case-insensitivity.

Also, if you pass in a parameter (say, $terms) containing the collection of target strings (e.g., ['Network', 'Phone', 'Smart']), the query can generate the corresponding regular expression. For example:

MATCH (s:Product {id:'5001207'})-->(o:ProdAttrs)
WHERE ANY(key in KEYS(o) WHERE
  key =~ '(?i).*(' + REDUCE(s=$terms[0], t IN $terms[1..] | s + '|' + t) + ').*')
RETURN o;

Upvotes: 0

Related Questions