在 tinkerpop gremlin 中,如何搜索存在对象列表的顶点的属性?

In tinkerpop gremlin, how do I search on properties of a vertex where there is a list of objects?

我有一个带有标签 MyVertex 的顶点,它具有以下属性

{
  text: "hello world",
  indicator: {
    code: [
      {
        unit: "thing",
        val: "123"
      },
      {
        unit: "other",
        val: "456"
      }
    ],
    text: "test"
  }
}

如何在code数组中找到所有具有某个val的顶点?

我正在使用 gremlin-node。

我尝试过如下查询但无济于事。

g.V()
 .hasLabel('MyVertex')
 .properties('indicator')
 .unfold()
 .has('val', '123')
 .toList()

要在 gremlin-node 中创建此图,我 运行 以下

const data = {
    text: 'hello world',
    indicator: {
        code: [
            {
                unit: 'thing',
                val: '123',
            },
            {
                unit: 'other',
                val: '456',
            },
        ],
        text: 'test',
    },
};

const v = g.addV('MyVertex');

for (const [key, val] of Object.entries(data)) {
    v.property(key, val);
}

v.next();

嗯,你可以这样做:

gremlin> g.addV('myvertex').property('text','hello world').property('indicator',[code:[[unit:'thing',val:123],[unit:'other',val:456]],text:'test']).iterate()
gremlin> g.V().hasLabel('myvertex').
......1>   filter(values('indicator').
......2>          select('code').
......3>          unfold().
......4>          select('val').is(12))
gremlin> g.V().hasLabel('myvertex').
......1>   filter(values('indicator').
......2>          select('code').
......3>          unfold().
......4>          select('val').is(123))
==>v[0]

请注意:

  1. 我想您目前正在使用 TinkerGraph,它可以支持这些复杂的(任意)属性 值。很多图表都不允许这样做,所以请注意,通过这种方式建模,您将被锁定在图表系统的一个子集中。
  2. 甚至 TinkerGraph 都不允许对此查询进行索引,因此它是一个完整的图形扫描,这意味着它会很昂贵。这可能不是 TinkerGraph 上最糟糕的查询,假设您可以使用索引适当地限制初始顶点搜索 space - 顶点标签可能还不够。

如果您需要搜索此类数据,我强烈建议您研究一种层次结构较少的模型来存储此类数据。