Search code examples
graph-databasesgremlintinkerpoptinkerpop3

How do I wrap each element in the list into an object?


I have the following gremlin query where I am querying multiple edges and then their accompanying verticies

     outE('childSolution')
                .inV()
                .local(union(
                     identity()
                     .valueMap(true)
                     .project('solution')
                    ,
                     outE('writtenBy')
                    .inV()
                    .valueMap(true)
                    .project('originator')
                    , 
                    outE('childProblem')
                    .inV()
                    .valueMap(true)
                    .project('childProblem')
                ))
                .fold()
                .project('childSolutions')

I am getting back

    "childSolutions": [
          { "solution" : { properties... }
          },
          {
            "originator": { properties... }
          }
     ]

what I'd like is

"childSolutions": [
        { "fullSolution : {
          { "solution" : { properties... }
          },
          {
            "originator": { properties... }
          }
         },
        { "fullSolution : {
          { "solution" : { properties... }
          },
          {
            "originator": { properties... }
          }
         }
     ]

Any help would be greatly appreciated


Solution

  • When asking questions about Gremlin, it is always best to supply a Gremlin script that can produce a sample graph so that it can be pasted into a Gremlin Console session. That way those who answer can test their results. In this case, I'll just take a swipe at re-writing your traversal, but I think you need to invert how you are using project() and you get a much more readable traversal and one that does what you want:

    out('childSolution').
    project('childSolutions').
      by(project('solution','originator','childProblem')
           by(valueMap(true)).
           by(out('writtenBy').valueMap(true)) 
           by(out('childProblem').valueMap(true)))
    

    With project() you are specifying up front what you want the keys to be and then using by() to say what should go in those keys. The output of this traversal doesn't exactly match what you want, but I think I got it right in spirit. If you want it to match exactly, then perhaps it would be best if you provided a sample graph in your question.