0

I think the title says it all. I'm building a social networking platform that allows for posting things and being able to create infinite sub posts (comments, and comments on comments). I can currently create the posts just fine, but I think for the GETS I need some sort of .repeat or recursive loop built in in order to keep finding subs-posts and then ultimately map them to c# classes (with nested lists of the sub-posts).

Dictionary<string, string> communityQueries = new Dictionary<string, string>
{
    {
        "GetPost",    "g.V('" + postId + "').as('mainpost')" +
                           ".repeat(outE().inV().hasLabel('post'))"
    }
};

This was my first go to, I've been working on a couple variations but haven't quite gotten it.

In this image you can see roughly what it looks like.

enter image description here

Pretty simplified, the end result needs to look like this:

{
     "Post (content)"
     "AuthorId"
     "Comments" : [
          {
              "Post":
              "AuthorId"
              "Comments"...
           }...
      ]
}
  • I recognize the .as() is doing nothing in that snippet as it is, it seems like I would need to use it though, not sure. – gabrielthursday Jul 20 '18 at 04:16
  • The `as()` is indeed not doing anything in the original query and should be removed. In fact, it's probably removed by the Gremlin query optimizer (see the output of chaining an `.explain()` at the end of any query). – jbmusso Jul 20 '18 at 07:07
  • @jbmusso thanks for that, that .explain() method looks really useful. – gabrielthursday Jul 20 '18 at 16:17

2 Answers2

2

Pictures are nice, but it's always best to ask questions about Gremlin by providing a small Gremlin script that generates some sample data. A script can really help frame the context of the question and get you a tested and working piece of code in the answer (example).

Without that I have to guess a little bit as your diagram does not have edge labels or property names, but to get the structure you want, you probably need to use project():

g.V(postId).
  project('post','author','comments').
    by('content').
    by(out('author').values('name'))
    by(repeat(out('post')).emit().values('content').fold())

So, with project() you define the keys of the Map that you will transform the Vertex identified by the "postId" into. The values that go into those keys are specified by the by() modulators that follow and map in the order of the keys. So to get the "post" we get the "content" property of the vertex. To get the "author" we traverse your "author" edge to a "person" vertex and extract the name. To get the "comments" I just assumed that you wanted the content from each comment and extracted them all. You might want to use order() to sort those a bit better or perhaps you want to project() each of those into its own map that contains similar data to the original post. Whatever you do, you have the pattern now to implement to this kind of transformation.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
0
g.V('p1').as('a').union(select('a'), out('author'), out('comments'))

This will have all the data you need.

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
Jayanta Mondal
  • 436
  • 2
  • 5