L.Rex
L.Rex

Reputation: 467

Regular expression on the string of a property name in Cypher

I know it is possible to use regular expressions for property values like for example:

MATCH (n)
WHERE n.SomeProperty =~ 'somestring*'
RETURN n;

What i want is to use regular expression on the property name and check for all the properties which start with a certain string like for example:

MATCH (n)
WHERE n.`SomeProperty*` > 10
RETURN n;

So I want to have all nodes which have a property which begins with 'SomeProperty' and have a value > 10 for this property.

This doesn't seems possible with using regular expressions like in my example. I've tried it and with my research i couldn't find a solution. Does anyone have an idea how to achieve this using another technique ?

Upvotes: 1

Views: 554

Answers (2)

jose_bacoy
jose_bacoy

Reputation: 12714

I created sample nodes as below:

Create (n1:RexNode {someproperty10: 10}),
   (n2:RexNode { someproperty11: 11}),
  (n3:RexNode {someproperty12: 12})

Then I used this query to return n2 and n3. As you can see n1 starts with someproperty but the value is not greater than 10. The quantifier is ANY so it will only look for at least one property (key of node n) and it will return it.

MATCH (n)  
WITH n  
   WHERE 
     ANY( k in  keys(n) 
          WHERE k STARTS WITH 'someproperty' 
            AND n[k] > 10
        )
RETURN n

Result:

╒═════════════════════╕
│"n"                  │
╞═════════════════════╡
│{"someproperty11":11}│
├─────────────────────┤
│{"someproperty12":12}│
└─────────────────────┘

Upvotes: 1

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

Given the following test graph

CREATE (:TestNode {somePropertyOne: 10})
CREATE (:TestNode {somePropertyTwo: 11})
CREATE (:TestNode {somePropertyThree: 12})
CREATE (:TestNode {someOtherProperty: 13})

The following query achieves what you want

MATCH (n)
WHERE ANY(x IN keys(n) WHERE x STARTS WITH 'someProperty' AND n[x] > 10)
RETURN n

╒════════════════════════╕
│"n"                     │
╞════════════════════════╡
│{"somePropertyTwo":11}  │
├────────────────────────┤
│{"somePropertyThree":12}│
└────────────────────────┘

Bear in mind that its really not an optimized query for graphs, so it will be slow on decent size databases.

Upvotes: 1

Related Questions