neo4j/gremlin/cypher:如何获取所有节点,直到我达到类似地图的设置中的一定距离(深度)?

StackOverflow https://stackoverflow.com/questions/8804966

  •  26-10-2019
  •  | 
  •  

我有一个带有字段的简单图 - 每个字段都有4个邻居(东北西北):

@NodeEntity
public class Field {
    @GraphId
    Long id;
    Field north;
    Field east;
    Field south;
    Field west;
    //.. other stuff
}

我设置了一个图DB(NEO4J),因此它们都很好并且连接(例如网格)。我现在想做的是从启动节点中获取所有节点 - IE 5啤酒花。

经典遍历这可以正常工作,看起来像这样:

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;
}

因此,如果我有这样的“地图”:

  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 

我询问启动节点'13''和一个2的“深度”,我需要获得标记的12个节点 - 因为它们是13个距离的2个步骤。

我在这个...中使用了Jo4neo ...

如何在Cypher或Gremlin中实现相同的功能?

我找到了朋友的示例,但这并没有真正帮助我,因为我需要深度成为参数(即在某些情况下,我想要4,有时6)。

笔记: 除字段外,还有其他连接,但我显然只需要字段。另外,我需要获取所有连接的节点 - 不仅是一个方向。

解决方案: 多亏了指针,我得到了解决方案:

start n=node(13) match n-[*1..2]->b where b.__type__ = "model.Field" return distinct b
有帮助吗?

解决方案

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

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

如果您完全想要3个步骤:

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

希望这可以帮助!

其他提示

该怎么办才能找到深度?意味着 *1..3 显示“ A-> B,A-> B-> C&A-> B-> C-> D”,这是由于Andres的Cypher查询结果,我想找到一个深度计数,即IE,1,1 a-> b的情况; 2在A-> b-> c&等方面。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top