【问题标题】:proper result using gremlin query使用 gremlin 查询的正确结果
【发布时间】:2013-09-13 18:16:22
【问题描述】:

在 neo4j 中,我创建了 2 个节点作为 CP 和 ANZ。我创建了两条边作为 Sell 和 Buy 在此之间,该边的 tx_amount 属性分别为 100 和 200。

cp --> 卖出 -> 100 -> ANZ

ANZ--> 买 -> 200 -> CP

现在我想获取卖方,买方,tx_amount。所以,如果我选择CP作为卖方。然后它应该打印如下:

==> [seller:CP, tx_amount:100, buyer:ANZ]
==> [seller:ANZ, tx_amount:200, buyer:CP]

在上面的结果中看到第一行返回有效输出,第二行的买方也正确。唯一的问题是第二行的卖方不是 ANZ,而是 CP。那么,如何解决这个问题。

当前查询,输出如下:

gremlin> g.v(0).outE().inV.as('seller').bothE('Sell','Buy').as('tx_amount').inV.as('buyer')。选择{it.name}{it.amount}{it.name}.sort{it[2]}

        ==> [seller:CP, tx_amount:100, buyer:ANZ]
        ==> [seller:CP, tx_amount:200, buyer:CP]

【问题讨论】:

  • 无法一次性确定卖家和买家的姓名。:(
  • 我建议您添加更多上下文并描述您的问题,以便其他人能够理解,否则您将被否决或问题被关闭。
  • 我想同时显示特定选定节点的供应商和买方。在我的情况下,由于 bothE(),买方正在发生变化......任何其他方式都可以使用循环或返回(?)或 ifelsethen 条件或反向概念。比如 if(bothE('Sell')) { print [供应商 = 左节点,买家 = 右节点] } else { print [供应商 = 右节点,买家 = 左节点] }

标签: neo4j cypher gremlin


【解决方案1】:

我通过将 headCustomerName 设置为 root,然后在 headCustomer 和 childcustomer 之间定义一个 child_rel 来做到这一点。

g.V.has("headCustomerName","ABC Corp.").inE('child_rel').outV().as('buyer').outE('Sell_To').as('tx_amount').inV().as('seller')  \
      .select{it.customerName}{it.amount}{it.customerName}  
==>[buyer:CP, tx_amount:100, seller:ANZ]
==>[buyer:CP, tx_amount:200, seller:SS Tech]
==>[buyer:SAK Corp., tx_amount:400, Supplier:AB Infotech]
...

和,

g.V.has("headCustomerName","ABC Corp.").inE('child_rel').outV().as('seller').inE('Sell_To').as('tx_amount').outV().as('buyer')    \
      .select{it.customerName}{it.amount}{it.customerName}  
==>[seller:CP, tx_amount:1100, buyer:NEW Int]
==>[seller:CP, tx_amount:1300, buyer:Marry Gold]
==>[seller:SAK Corp., tx_amount:1006, buyer:LLI Corp.]
...

谢谢。 :)

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多