user3520220
user3520220

Reputation: 1

How to change a node's property based on one of its other properties in Neo4j

I just started using Neo4j server 2.0.1. I am having trouble with the writing a cypher script to change one of the nodes property to something based one of its already defined properties.

So if I created these node's:

CREATE (:Post {uname:'user1', content:'Bought a new pair of pants today', kw:''}),
       (:Post {uname:'user2', content:'Catching up on Futurama', kw:''}),
       (:Post {uname:'user3', content:'The last episode of Game of Thrones was awesome', kw:''})

I want the script to look at the content property and pick out the word "Bought" and set the kw property to that using a regular expression to pick out word(s) larger then five characters. So, user2's post kw would be "Catching, Futurama" and user3's post kw would be "episode, Thrones, awesome".

Any help would be greatly appreciated.

Upvotes: 0

Views: 112

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

You could do something like this:

MATCH (p:Post { uname:'user1' })
WHERE p.content =~ "Bought .+"
SET p.kw=filter(w in split(p.content," ") WHERE length(w) > 5)

if you want to do that for all posts, which might not be the fastest operation:

MATCH (p:Post)
WHERE p.content =~ "Bought .+"
SET p.kw=filter(w in split(p.content," ") WHERE length(w) > 5)
  • split splits a string into a collection of parts, in this case words separated by space
  • filter filters a collection by a condition behind WHERE, only the elements that fulfill the condition are kept

Probably you'd rather want to create nodes for those keywords and link the post to the keyword nodes.

Upvotes: 1

Related Questions