【问题标题】:In tinkerpop gremlin, how do I search on properties of a vertex where there is a list of objects?在 tinkerpop gremlin 中,如何搜索存在对象列表的顶点的属性?
【发布时间】:2019-08-21 15:13:45
【问题描述】:

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

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

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

我正在使用 gremlin 节点。

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

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();

【问题讨论】:

  • 你能提供一个脚本来创建一个小示例图吗?
  • 当然!我编辑了原始帖子。
  • 我不认为你可以这样看待“val”值,据我所知,gremlin 只支持 1 级元属性。所以保存在“指标”内的所有对象都不会保存为顶点属性,因此您不能通过它们进行过滤。我认为您应该考虑将数据分成几个连接的顶点。也不是所有的图形数据库都支持元属性(如 Neptune)。
  • @noam621 感谢您的回复。在这种情况下,元属性是什么?我相信您可以将复杂的对象保存为顶点属性。至少我能够保存这个结构并再次查询它并检查地图indicatorcode的子地图。
  • 可以获得二级和三级属性。但它们没有被索引。所以我认为你不能对它们做任何小鬼步骤。

标签: node.js gremlin tinkerpop


【解决方案1】:

好吧,你可以这样做:

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 上最糟糕的查询 - 顶点标签可能还不够。

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

【讨论】:

  • 谢谢!这是很好的信息,将帮助我决定如何存储我的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多