【问题标题】:OrientDB: traverse only edges with some propertyOrientDB:仅遍历具有某些属性的边
【发布时间】:2016-10-26 02:23:20
【问题描述】:

我想从一个顶点开始遍历,只沿着匹配特定表达式的边(例如:friendsWith.type="bestFriend")。我该怎么做呢?

此外,我必须对一堆顶点运行此操作。是否可以通过遍历的“头”来组织事物?理想的回报是:

[[start1, [list of all the vertices during traversal], [start2, [...]],...]

【问题讨论】:

  • 你能更好地解释你想要得到什么吗? start2 应该是什么?你能举个例子吗?
  • 你能接受一个javascript函数吗?
  • @AlessandroRota 实际上一个 javascript 函数会很有帮助。

标签: orientdb


【解决方案1】:

我用这张图试过

我使用这个带有参数rid的javascript函数

var g=orient.getGraph();
var nodes = [];
var previous=[];
var currently=[];
var b=g.command("sql","select from " + rid);
if(b.length>0){
    var vertex=b[0];
    previous.push(vertex);
    nodes.push(vertex);
    do{
        for(i=0;i<previous.length;i++){
            var vertexOut=previous[i];
            var edges=g.command("sql","select expand(outE('friendsWith')) from "+ vertexOut.getId());
            for(s=0;s<edges.length;s++){ 
                var edge=edges[s];
                var type= edge.getProperty("type");
                if(type=="bestFriend"){
                    var vertices=g.command("sql","select expand(inV('friendsWith')) from "+ edge.getId());
                    var vertex=vertices[0];
                    var found=false;          
                    for(k=0;k<nodes.length;k++){
                        var id=nodes[i].getId();
                        var id_v=vertex.getId()
                        if(id==id_v){
                            found=true;
                            break;
                        }
                    }
                    if(found==false){
                        currently.push(vertex);
                        nodes.push(vertex);
                    }
                }
            }
        }
        change();
    }while(previous.length>0);
    return nodes;
}

function change(){
    previous=[];
    for (indice=0;indice<currently.length;indice++)
        previous.push(currently[indice]);
    currently=[];
}

select expand(node) from (select myFunction("#9:0") as node) 我得到了

希望对你有帮助。

【讨论】:

  • 这看起来真不错!请问,你是否期望在服务器上用javascript调用这个函数比用python(pyorient)编写一个做同样事情(即调用SELECT命令并组织结果)的本地脚本要快得多。
  • 是的,你可以在服务器上用javascript调用这个函数
  • 我的意思是,JavaScript 函数是否与 SQL 调用一样快?客户端 python 脚本是否比两者都慢?
  • 我认为服务器端的javascript函数比客户端pyhon脚本快
【解决方案2】:

试试这个:

select outV().name as start, inV().name as end from(traverse * from friendsWith while type = "bestFriend")

不用遍历也可以。

希望对你有帮助。

问候。

【讨论】:

  • 遍历部分对我不起作用,不应该是traverse &lt;edge class&gt; from &lt;vertex&gt;吗?我试过traverse inE('friendsWith') from V while type="bestFriend",但什么也没得到。
  • 我认为,你不会从 V 中的 traverse inE('friendsWith') 中得到任何东西,而 type="bestFriend" 因为你的顶点没有属性类型
  • 好的,这更有意义 - 你是说我应该遍历边而不是顶点,这样“while”命令会为您提供边属性。你怎么能用 SELECT 做到这一点?说,只有两层。我知道你可以做select out('friendswith').out('friendswith') from E,但我不知道如何在那里指定边缘属性。
  • 您可以使用 WHERE 条件指定边缘属性
  • @MichelaBonizzi 是的,但是“where”条件是否适用于第一个或第二个或两个“out('friendswith')”组件?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多