[英]Map gremlin projection results
Imagine an item can be tagged by connecting unique tag
nodes to a post
with the twist that tags come from different sources.想象一下,可以通过将唯一的tag
节点连接到post
来标记一个项目,标签来自不同的来源。 The source is recorded on the connecting edge, and there may be only one edge between a post and tag corresponding to the same source.源记录在连接边上,同一源对应的帖子和标签之间可能只有一条边。 This query returns posts and their tags with the tag containing details of source and score:此查询返回帖子及其标签,标签包含来源和分数的详细信息:
project('title', 'tags').by('title').
by(inE().as('rel').
hasLabel('tags').project('value', '_rel').
by(outV().hasLabel('tag').values('value')).
by(project('score', 'source').
by('score').by('source')).
group().
by(values('value'))
)
The result is in the following shape:结果是下面的形状:
[
{
"title": "x",
"tags": {
"dog": [
{
"value": "dog",
"_rel": {
"score": 5,
"source": "user"
}
}
],
"cat": [
{
"value": "cat",
"_rel": {
"score": 3,
"source": "user"
}
},
{
"value": "cat",
"_rel": {
"score": 2,
"source": "google"
}
}
]
}
}
]
Question is: how can this shape be transformed to the more compact form below?问题是:如何将这个形状转化为下面更紧凑的形式?
{
"title": "x",
"tags": [
{
"value": "dog",
"_rel": [
{
"score": 5,
"source": "user"
}
]
},
{
"value": "cat",
"_rel": [
{
"score": 2,
"source": "google"
},
{
"score": 3,
"source": "user"
}
]
}
]
}
This is a typescript (and Neptune) implementation so some limitations apply.这是一个 typescript(和 Neptune)实现,因此存在一些限制。
A minimal sample graph is available at https://gremlify.com/khvizyzeqg and below:可在https://gremlify.com/khvizyzeqg及以下网址获得最小示例图:
property(single, 'value', 'cat').addV('tag').
as('2').
property(single, 'value', 'dog').addV('post').
as('3').
property(single, 'title', 'x').addE('tags').
from('1').to('3').property('score', 3).
property('source', 'user').
property('value', 'cat').addE('tags').
from('1').to('3').property('score', 2).
property('source', 'google').
property('value', 'cat').addE('tags').
from('2').to('3').property('score', 5).
property('source', 'user').
property('value', 'dog')
This first answer isn't quite what you asked for because the format doesn't exactly match the example you presented, however I think it's worth considering as usable since it offers a more compact data structure with the same content and the Gremlin is quite readable:第一个答案并不完全符合您的要求,因为格式与您提供的示例不完全匹配,但是我认为值得考虑使用它,因为它提供了具有相同内容的更紧凑的数据结构并且 Gremlin 非常可读:
gremlin> g.V().hasLabel('post').
......1> project('title', 'tags').
......2> by('title').
......3> by(inE().hasLabel('tags').
......4> group().
......5> by('value').
......6> by(project('score','source').
......7> by('score').by('source').
......8> fold()))
==>[title:x,tags:[cat:[[score:2,source:google],[score:3,source:mobius]],dog:[[score:5,source:mobius]]]]
This second answer produces output that should match your requested output, but as you can see it forces you to destructure the Map
above just to re-write it back to match the form you wanted:第二个答案产生 output,它应该与您请求的 output 相匹配,但如您所见,它迫使您解构上面的Map
,只是为了重新写回以匹配您想要的形式:
gremlin> g.V().hasLabel('post').
......1> project('title', 'tags').
......2> by('title').
......3> by(inE().hasLabel('tags').
......4> group().
......5> by('value').
......6> by(project('score','source').
......7> by('score').by('source').
......8> fold()).
......9> unfold().
.....10> project('value','_rel').
.....11> by(keys).
.....12> by(values).
.....13> fold())
==>[title:x,tags:[[value:cat,_rel:[[score:2,source:google],[score:3,source:mobius]]],[value:dog,_rel:[[score:5,source:mobius]]]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.