Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tree 如何在Neo4j中映射树结构?_Tree_Neo4j_Cypher - Fatal编程技术网

Tree 如何在Neo4j中映射树结构?

Tree 如何在Neo4j中映射树结构?,tree,neo4j,cypher,Tree,Neo4j,Cypher,我正在用Neo4j描述一个树结构的数据集。在我当前的模型中,一个节点可以有n个到其他节点的链接,使其成为这些节点的子节点: root | \-- A | \-- 1 | \-- 2 指向根目录的链接 1和2链接到 根是一个 1和2是一个家庭的孩子 因为我使用的是nodejs(with),所以只能使用Cypher读取数据库。要读取节点,我使用以下查询: START n=node(1) -- this is the root node MA

我正在用Neo4j描述一个树结构的数据集。在我当前的模型中,一个节点可以有n个到其他节点的链接,使其成为这些节点的子节点:

root
 |
 \-- A
     |
     \-- 1
     |
     \-- 2
  • 指向根目录的链接
  • 1和2链接到
  • 根是一个
  • 1和2是一个家庭的孩子
因为我使用的是nodejs(with),所以只能使用Cypher读取数据库。要读取节点,我使用以下查询:

START n=node(1)
    -- this is the root node

MATCH (n)<-[linkedby:links*]-x, x-[linksto?:links*1]->()
    -- get all nodes that link to the root node and all nodes linking to those nodes, etc, etc and retrieve all nodes those found nodes link to

RETURN n, x, linkedby, LAST(linkedby) AS parent, COLLECT(DISTINCT linksto) AS links      
    -- the last in the path of linkedby is the direct parent

ORDER BY length(linkedby), parent.rank?
    -- ordering so that parents are fetched before their children from the result set
服务S由T组管理:

T
|
\-- S
团队T是组织O的一部分:

O
|
\-- T
树:

2) 我的数据在:

所以,前几天我和他进行了一次谈话,他承认Cypher在处理递归查询方面(目前)并不擅长。这应该在2.1中有所改变,但在此之前,他建议我编写一个,然后我可以从nodejs调用它

这完全解决了我的问题:使用纯Java检索树比使用Cypher快10倍左右

简化代码:

public class EntitiesStream {
    public Entity load(long nodeId) {
        Node n = database.getNodeById(nodeId);
        Entity entity = Entity.from(n);
        loadChildren(n, entity);
        return entity;
    }

    private void loadChildren(Node n, Entity e) {
        for (Relationship linksFrom: n.getRelationships(Direction.INCOMING, Relationships.links)) {
            Node startNode = linksFrom.getStartNode();
            Entity childEntity = Entity.from(startNode);
            e.addChild(((Number) linksFrom.getProperty("rank")).longValue, childEntity);
            this.loadChildren(startNode, childEntity);
        }
    }
}

public class Entity {
    private final TreeMap<Long, Entity> sortedChildren;

    Entity(long dbId) {
        this.sortedChildren = new TreeMap<>();
        // ...
    }

    public static Entity from(Node node) {
        Entity e = new Entity(node.getId());
        // ...

        return e;
    }

    public void addChild(Long rank, Entity childEntity) {
        sortedChildren.put(rank, childEntity);
    }
}
公共类实体流{
公共实体加载(长节点ID){
Node n=database.getNodeById(nodeId);
实体=实体。从(n);
loadChildren(n,实体);
返回实体;
}
私有void loadChildren(节点n,实体e){
for(关系链接from:n.getRelationships(Direction.INCOMING,Relationships.links)){
Node startNode=linksFrom.getStartNode();
实体子实体=实体.from(startNode);
e、 addChild(((编号)linksFrom.getProperty(“rank”)).longValue,childEntity);
this.loadChildren(startNode,childEntity);
}
}
}
公共类实体{
私人最终树状图分类儿童;
实体(长dbId){
this.sortedChildren=新树映射();
// ...
}
来自(节点)的公共静态实体{
实体e=新实体(node.getId());
// ...
返回e;
}
public void addChild(长列,实体childEntity){
已分类的子项put(等级、子项实体);
}
}

1。你能提供你试图解决的现实世界的问题吗。2.在console.neo4j.org中设置您的数据,并共享(顶部的按钮)链接。有趣的问题,遗憾的是,没有人在线回答或提示一些代码示例是的,人们不再在这里回答真正的问题了:D对农场声誉来说只是微不足道的问题。我可能会马上回答这个问题。它在2.1中有没有变化?我自己也很好奇,我用Neo4j 2.3.1测试了我的应用程序,与2.0.3相比,Cypher查询快了大约20%。尽管如此,没有任何地方接近性能的扩展。
root
 |
 |
 +-- Processes
 |   |
 |   +-- P1
 |   |   |
 |   |   \-- S
 |   |
 |   \-- P2
 |   |   |
 |   |   \-- S
 |
 +-- Organisations
 |   |
 |   +-- O
 |       |
 |       \-- T
 |           |
 |           \-- S
CREATE 
  (r {name:'root'}), 
  (ps {name: 'Processes'})-[:links]->r, 
    (p1 {name: 'P1'})-[:links]->ps,
    (p2 {name: 'P2'})-[:links]->ps,
      (s {name: 'Service'})-[:links]->p1,
      s-[:links]->p2,
  (org {name: 'Organisations' })-[:links]->r,
    (t {name: 'Team'})-[:links]->org,
      s-[:links]->t
public class EntitiesStream {
    public Entity load(long nodeId) {
        Node n = database.getNodeById(nodeId);
        Entity entity = Entity.from(n);
        loadChildren(n, entity);
        return entity;
    }

    private void loadChildren(Node n, Entity e) {
        for (Relationship linksFrom: n.getRelationships(Direction.INCOMING, Relationships.links)) {
            Node startNode = linksFrom.getStartNode();
            Entity childEntity = Entity.from(startNode);
            e.addChild(((Number) linksFrom.getProperty("rank")).longValue, childEntity);
            this.loadChildren(startNode, childEntity);
        }
    }
}

public class Entity {
    private final TreeMap<Long, Entity> sortedChildren;

    Entity(long dbId) {
        this.sortedChildren = new TreeMap<>();
        // ...
    }

    public static Entity from(Node node) {
        Entity e = new Entity(node.getId());
        // ...

        return e;
    }

    public void addChild(Long rank, Entity childEntity) {
        sortedChildren.put(rank, childEntity);
    }
}