graph - Neo4j/Gremlin/Cypher: how to get all nodes until i reach a certain distance (depth) in a map-like setup? -



graph - Neo4j/Gremlin/Cypher: how to get all nodes until i reach a certain distance (depth) in a map-like setup? -

i have simple graph fields - each fields has 4 neighbours (north-east-south-west):

@nodeentity public class field { @graphid long id; field north; field east; field south; field west; //.. other stuff }

i graph db (neo4j) set these nice , connected (like grid). want nodes - starting node - i.e. 5 hops away.

with classic traverses works fine , looks this:

public collection<field> getfieldswithdepth(field startfield, final int depth) { node fieldnode = this.getgraphdb().getnodebyid(startfield.getid()); traverser traverser = fieldnode.traverse(traverser.order.breadth_first, // check direct relations first, go deeper new stopevaluator() { @override public boolean isstopnode(traversalposition pos) { if (pos.depth()==depth) homecoming true; else homecoming false; } }, // worst case: go end of graph new returnableevaluator() { @override public boolean isreturnablenode(traversalposition pos) { homecoming true; } }, relations.north, direction.outgoing, relations.east, direction.outgoing, relations.west, direction.outgoing, relations.south, direction.outgoing ); arraylist<field> fields = new arraylist<field>(); (node node : traverser.getallnodes()) { fields.add((field)this.getgraph().get(node)); } homecoming fields; }

so if have "map" this:

1 2 | 3| 4 5 6 |7| | 8| | 9| 10 |11| |12| *13* |14| |15| 16 |17| |18| |19| 20 21 22 |23| 24 25

and query starting node ''13'' , "depth" of 2 need marked 12 nodes - since 2 steps aways 13.

i used jo4neo in this...

how can accomplish same functionality in cypher or gremlin?

i found friend-of-friend example, not help me, because need depth parameter (i.e. in cases want depth of 4, 6).

note: there other connections except fields, need fields. need connected nodes - not in 1 direction.

solution: pointer got solution:

start n=node(13) match n-[*1..2]->b b.__type__ = "model.field" homecoming distinct b

http://docs.neo4j.org/chunked/snapshot/query-match.html#match-variable-length-relationships

start a=node(3) match a-[:friend*1..3]->x homecoming a,x

if want 3 steps:

match a-[:friend*3..3]->x

hope helps!

graph neo4j cypher

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -