Reputation: 789
I have nodes with a property called 'flavors' which includes tuples of data like this:
node1.flavors = {"cherry":174,"vanilla":105,"chocolate":60}
node2.flavors = {"cherry":17,"vanilla":10,"chocolate":300}
I want to find the node with the highest integer associated with chocolate, which would be node2. How can I do this?
I figured out how to use a regex to get those where chocolate is in the flavors property:
start n=node(*)
WHERE has(n.flavors) AND n.flavors =~ '.*chocolate.*'
RETURN count(n);
Now I just need to get the number after "chocolate":_.
MORE INFO: It's a JSON string. If I can't deal with it in cypher I'll have to deal with it in Ruby and re-do the data model in neo4j. I believe I can't use substring because we don't have an indexOf function on properties, right?
2.0.0p247 :002 > r = JSON.parse('{"vanilla":161,"chocolate":21,"cherry":18}')
=> {"vanilla"=>161, "chocolate"=>21, "cherry"=>18}
2.0.0p247 :004 > r.class
=> Hash
2.0.0p247 :005 > r.keys
=> ["vanilla", "chocolate", "cherry"]
2.0.0p247 :006 > r.values
=> [161, 21, 18]
2.0.0p247 :007 > r["vanilla"]
=> 161
Upvotes: 0
Views: 192
Reputation: 489
The query you provide will check every single node in the graph, which will get slow as the size of the graph increases. You're trying to use Cypher to do string manipulation, while Cypher is first and foremost a language to query the graph. It would seem to me you're either using the wrong datastore or your not modelling it usefully for a graph. Why not make the nodes (recipies?) and flavours entities and connect them with a relationship that has a property on how much of the flavour they have? Then you can query them easily and efficiently. I've quickly modelled this here: http://console.neo4j.org/r/tp355x (uses Cypher/Neo4j 2.0).
So, the answer to the question would be not to try to use Cyhper for something it wasn't really made for, but redo the model in your Ruby code like demoed above.
If you need inspiration on how to represent data in a graph, there are Graph Gists to give you some ideas perhaps.
Upvotes: 3