簡體   English   中英

CosmosDB Gremlin:在隨后的where過濾器中使用存儲的值

[英]CosmosDB Gremlin: Using stored value in subsequent where filter

嘗試根據存儲的字符串列表進行過濾時遇到問題。 我想在sideEffect()構建此列表,然后在隨后的where(without())使用它。 在下面的示例中,我希望用戶列表不包括user.a1 ,但user.a1包括user.a1

g.V().hasLabel('user').sideEffect(hasId('user.a1').id().store('exclude')).where(id().is(without('exclude')))
[
    {
        "id": "user.a1",
        "label": "user",
        "type": "vertex"
    },
    {
        "id": "user.b1",
        "label": "user",
        "type": "vertex"
    },
    ...
]

我以為這是因為Gremlin正在檢查文字字符串exclude ,所以我如何讓它檢查存儲在exclude變量中的ID?

例如,這有效:

g.V().hasLabel('user').sideEffect(hasId('user.a1').id().store('exclude')).where(id().is(without('user.a1')))
[
    {
        "id": "user.b1",
        "label": "user",
        "type": "vertex"
    },
    ...
]

編輯#1

嘗試了由stephen mallette提出的查詢,但仍然沒有得到我想要的東西(也許在CosmosDB上不可能?)。

首先,顯然, filter在CosmosDB上不是可用的功能:

g.V().hasLabel('user').
  sideEffect(hasId('user.a1').id().store('exclude')).
  filter(id().where(without('exclude')))

ExceptionType : GraphCompileException
ExceptionMessage : Gremlin Query Compilation
Error: Unable to find any method 'filter' @ line 1, column 81. 1
Error(s) Source : Microsoft.Azure.Graphs

第二個建議導致空了,而第二個建議起作用了:

g.V().hasLabel('user').hasId('user.a1').
  aggregate('exclude').where(without('exclude'))
[]

g.V().hasLabel('user').
  sideEffect(hasId('user.a1').aggregate('exclude')).
  where(without('exclude'))
[
  {
    "id": "user.b1",
    "label": "user",
    "type": "vertex"
  },
  {
    "id": "user.b2",
    "label": "user",
    "type": "vertex"
  },
  ...
]

問題是我想使用邊緣的屬性值過濾邊緣。 我的現實生活狀況是,如果可能的話,我希望將以下兩個查詢組合為一個查詢:獲取一組role頂點ID,然后獲取擁有其中一個角色並屬於組子集的用戶。 (用戶屬於一個組,並且其角色ID在該組中被設置為user-> group belongs_to edge的屬性。)

  1. 從角色層次結構( role.2 +所有父級)中獲取一組角色
g.V('role.2').store('roles').
  repeat(__.out('is_under')).emit().store('roles').
  select('roles').unfold().dedup().id()
[
    "role.2",
    "role.1"
]
  1. 將具有上述角色的用戶分配到group.B或以上的用戶中,並為他們提供其他角色的優勢。
g.V('group.B').
  sideEffect(inE('belongs_to').
    has('role',within('role.1','role.2')).outV().store('users')).
  repeat(__.out('belongs_to')).emit().inE('belongs_to').
    has('role',within('role.1','role.2')).outV().store('users').
  select('users').unfold().addE('has').to(g.V('role.12'))

這是一種方法:

g.V().hasLabel('user').
  sideEffect(hasId('user.a1').id().store('exclude')).
  filter(id().where(without('exclude')))

您可以根據創建的示例與實際需求的匹配程度或是否因問題而對其進行了簡化來進行一些更改。 首先,您可以將sideEffect()因為從sideEffect() store()本身就是一個副作用。 其次,我想知道您是否真的想要store()那里沒有一個aggregate() -在這里,聚合將在繼續進行過濾之前貪婪地收集所有id:

g.V().hasLabel('user').
  hasId('user.a1').aggregate('exclude').by(id).
  filter(id().where(without('exclude')))

當然,這有點像“不執行任何操作”遍歷,因為您已經使用hasId('user.a1')過濾掉了不需要的頂點,但是同樣,我假設您在查詢中具有更大的復雜性在游戲中比您所展示的要好。 我只是指出Gremlin力學。

如果您不擔心id,也可以進一步簡化-頂點的相等性將與id相同,因此,以下內容與上述內容相同,但Gremlin步驟更少:

g.V().hasLabel('user').
  hasId('user.a1').aggregate('exclude').
  where(without('exclude'))

當詢問有關Gremlin的問題時,最好添加一個生成示例數據的腳本 ,以便您可以進行符合您需求的經過測試的遍歷。 取而代之的是,我修改了“現代”圖以包含“重量”標簽:

gremlin> g.addV('weight').property('v',1.0d)
==>v[13]

其中“ v”值與玩具圖的現有邊緣的兩個權重值匹配。 沒有filter() ,我認為您會陷入這種模式:

gremlin> g.V().hasLabel('weight').
......1>   aggregate('w').by('v').
......2>   V().hasLabel('person').
......3>   outE().as('x').values('weight').
......4>   where(within('w')).
......5>   select('x')
==>e[8][1-knows->4]
==>e[10][4-created->5]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM