简体   繁体   English

Neo4j遍历子图

[英]Neo4j traversal for subgraph

I want to extract the subgraph from Neo4j graph database.The input will be two nodes. 我想从Neo4j图数据库中提取子图。输入将是两个节点。 we need to find all the one-hop neighbors from both nodes and form the union of nodes(let's call it closure). 我们需要从两个节点中找到所有一跳邻居并形成节点的并集(让我们称之为闭包)。 Now we have to get all the relationships in this closure (union of nodes including input nodes). 现在我们必须获得此闭包中的所有关系(包括输入节点的节点的并集)。 how to approach this problem in neo4j with java "efficiently". 如何使用java“有效”地在neo4j中解决这个问题。 please suggest. 请建议。

Something like this? 像这样的东西?

// find closure
Set<Node> nodes = new HashSet<>();
for (Node n : inputs) {
  for (Relationship rel : n.getRelationships()) {
      nodes.add(r.getOtherNode(n));
  }
}
nodes.addAll(inputs);

// find rels withing closure
Set<Relationship> rels = new HashSet<>();
for (Node n : nodes) {
  for (Relationship rel : n.getRelationships()) {
      if (nodes.contains(r.getOtherNode(n))) rels.add(rel);
  }
}

return rels;

it's taking lot of time while getting the rels within closure. 在关闭内部时,需要花费大量时间。 I gave 2 nodes as inputs where the closure is of size is 2300. For this input, it is taking around 2minutes to get the data. 我提供了2个节点作为输入,其中大小为2300.对于此输入,大约需要2分钟来获取数据。 But we need the data in few seconds Total nodes in Neo4j : 20000 nodes. 但我们需要几秒钟内的数据Neo4j中的总节点数:20000个节点。 Total rels in Neo4j : 360000 edges The following is the code used to get the data. Neo4j中的Total rels:360000 edge以下是用于获取数据的代码。

public void getSemanticContext() { public void getSemanticContext(){

    List<Node> inputs = new ArrayList<>();
    List<Integer> qterms1 = new ArrayList<>();
    Set<Node> nodes = new HashSet<>();
    Set<Integer> closure = new HashSet<>();
    rdbms.createConnection();

    RandomWalk_RDBMS rwalk = new RandomWalk_RDBMS(rdbms, operation);
    Map<Integer, Double> sortedTerms=null;


    Transaction tx = graphDb.beginTx();

     try
     {
        ResourceIterator<Node> id1 =  graphDb.findNodesByLabelAndProperty( DynamicLabel.label("concept"), "name", "copy" ).iterator();
        ResourceIterator<Node> id2 =  graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "concept"), "name", "main" ).iterator();

        if ( id1.hasNext() )
        {
            firstNode = id1.next();
        }
        if ( id2.hasNext() )
        {
            secondNode = id2.next();
        }
        System.out.println(firstNode.getProperty("id"));
        System.out.println(firstNode.getProperty("name"));

        // find closure

        qterms1.add(Integer.parseInt(firstNode.getProperty("id").toString()));
        qterms1.add(Integer.parseInt(secondNode.getProperty("id").toString()));
        inputs.add(firstNode);
        inputs.add(secondNode);

        closure.add(Integer.parseInt(firstNode.getProperty("id").toString()));
        closure.add(Integer.parseInt(secondNode.getProperty("id").toString()));
        for (Node n : inputs) 
        {
          for (Relationship rel : n.getRelationships()) {
              nodes.add(rel.getOtherNode(n));
              closure.add(Integer.parseInt(rel.getOtherNode(n).getProperty("id").toString()));
          }
        }
        nodes.addAll(inputs);
        System.out.println("closure generated");
        System.out.println(nodes);
        genScoreMap= new HashMap<>();
        // find rels withing closure
        Set<Relationship> rels = new HashSet<>();
        for (Node n : nodes) {
            HashMap<Integer, Double> innerMap = new HashMap<Integer, Double>();
          for (Relationship rel : n.getRelationships()) {
              if (nodes.contains(rel.getOtherNode(n))) 
                  {
                    rels.add(rel);
                    innerMap.put(Integer.parseInt(rel.getOtherNode(n).getProperty("id").toString()), Double.parseDouble(rel.getProperty("generatability_score").toString()));
                  }
              genScoreMap.put(Integer.parseInt(n.getProperty("id").toString()), innerMap);
          }
        }

     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     tx.close();
    // System.out.println(genScoreMap);
     System.out.println("semantic context generated");
     sortedTerms=rwalk.randomWalk(qterms1,genScoreMap,closure);
    printResult(sortedTerms,rwalk);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM