Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:43+00:00 2026-05-28T01:46:43+00:00

I have a simple graph with fields – each fields has 4 neighbours (north-east-south-west):

  • 0

I have a simple graph with 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 a graph db (neo4j) set up so these are all nice and connected (like a grid). What I want to do now is get all nodes – from a starting node – which are i.e. 5 hops away.

With a classic traverses this works fine and looks like 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, then go deeper 
            new StopEvaluator() {

                @Override
                public boolean isStopNode(TraversalPosition pos) {
                    if (pos.depth()==depth)
                        return true;
                    else
                        return false;
                }
            },  // worst case: go to end of graph 
            new ReturnableEvaluator() {

                @Override
                public boolean isReturnableNode(TraversalPosition pos) {
                    return true;
                }
            },
            Relations.north,    
            Direction.OUTGOING,
            Relations.east,
            Direction.OUTGOING,
            Relations.west,
            Direction.OUTGOING,
            Relations.south,
            Direction.OUTGOING
            );

    ArrayList<Field> fields = new ArrayList<Field>();
    for (Node node : traverser.getAllNodes())
    {
        fields.add((Field)this.getGraph().get(node));
    }
    return fields;
}

So if I have a “map” like 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 I query with starting node ”13” and a “depth” of 2 I need to get the marked 12 nodes – since they are 2 steps aways from 13.

I used jo4neo in this…

How can I achieve the same functionality in cypher or gremlin?

I found the friend-of-friend example, but this does not really help me, because I need the depth to be a parameter (i.e. in some cases I want a depth of 4, sometimes 6).

Note: there are also other connections except Fields, but I obviously just need the fields. Also I need to get all connected nodes – not just in one direction.

Solution: Thanks to the pointer I got the solution:

start n=node(13) match n-[*1..2]->b where b.__type__ = "model.Field" return distinct b
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:46:44+00:00Added an answer on May 28, 2026 at 1:46 am

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

    START a=node(3)
    MATCH a-[:FRIEND*1..3]->x
    RETURN a,x
    

    If you want exactly 3 steps:

    MATCH a-[:FRIEND*3..3]->x
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple Graph class, where for each Node , I store
I have simple class with width and height member fields which define number of
our task is quite simple, we have an object graph where each object (IDItem)
I have a simple weighted graph A 1 / \\ 0.5 / \\0.5 B
I have a very simple line graph I'm trying to create in gRaphael. My
I have this simple graph: name -> string ^ | v label let matrix
I am using java2d to draw a simple graph at the moment I have
So I have made this simple interface: package{ public interface GraphADT{ function addNode(newNode:Node):Boolean; }
I have a simple graph with a lot of points, so I don't want
I have a simple JSON object graph I'm trying to render using a Mustache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.