Search code examples
graphtitangremlinvertexrexster

How to get Post (Vertex) from all users whom I've followed (Edge) in Titan using Gremlin


I'm getting list of all users whom I've followed using following query :-

g.v(2304).out('Follow')

Response :

{

"success": true,
"results": 

[

{

"Type": "User",
"CreatedTime": "2016-03-16T18:39:48.5000845Z",
"Username": "[email protected]",
"FirstName": "Joany",
"CoverImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/947174ae-3e60-4dd0-877f-cc988fae3888.jpg",
"ImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/f4f6901c-64c4-425b-929b-43936c53eaba.png",
"LastName": "Ashtana",
"Gender": "male",
"_id": ​768,
"_type": "vertex"

},
{

"Type": "User",
"CreatedTime": "2016-04-05T16:36:49.3740440Z",
"Username": "[email protected]",
"FirstName": "Maria",
"CoverImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/images/companyRectangleImageNotAvailable.png",
"ImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/users/female_professional.png",
"LastName": "Sharapova",
"Gender": "female",
"_id": ​5120256,
"_type": "vertex"

},
{

"Type": "User",
"CreatedTime": "2016-04-12T07:42:18.8036554Z",
"Username": "[email protected]",
"FirstName": "Abhi",
"CoverImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/afd0d047-0d1c-4391-8f88-95f7775c615f.jpg",
"ImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/fd0842d4-579b-4896-9525-945f8f718816.JPG",
"LastName": "Sri",
"Gender": "male",
"_id": ​12800512,
"_type": "vertex"

},
{

"Type": "User",
"CreatedTime": "2016-04-15T07:51:34.3867249Z",
"Username": "[email protected]",
"FirstName": "Goop",
"CoverImageUrl": "",
"ImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
"LastName": "Chup",
"Gender": "NA",
"_id": ​17928960,
"_type": "vertex"

},
{

"Type": "User",
"CreatedTime": "2016-04-20T03:25:30.0753729Z",
"Username": "[email protected]",
"FirstName": "Kunal",
"CoverImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/ee167d15-52bb-4f4f-99fb-038f27eee1dc.jpeg",
"ImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
"LastName": "Sharma",
"Gender": "NA",
"_id": ​23041024,
"_type": "vertex"

}
],
"version": "2.5.0",
"queryTime": ​21.94298

}

And I'm getting particular user post using following query :

g.v(396800).in('WallPost').sort{ a, b -> b.PostedTime <=> a.PostedTime }.()[0..3].transform{ [postInfo : it, commentsInfo: it.in('Comment').sort{ a, b -> b.PostedTime <=> a.PostedTime }.()[0..5].transform{[commentInfo:it, commentedBy: it.in('Created')]},userInfo:it.in('Created')] }

And I'm getting response :

{
"success": true,
"results": [
{
  "postInfo": {
    "PostImage": "",
    "Type": "Post",
    "PostedByUser": "[email protected]",
    "PostedTime": "2016-04-29T11:48:17.5069984Z",
    "PostedTimeLong": ​635975272975069984,
    "PostMessage": "testing new post. on pooja",
    "_id": ​30729984,
    "_type": "vertex"
  },
  "commentsInfo": [

  ],
  "userInfo": [
    {
      "Type": "User",
      "CreatedTime": "2016-04-20T03:25:30.0753729Z",
      "Username": "[email protected]",
      "FirstName": "Kunal",
      "CoverImageUrl": "https://s3-ap-southeast-1.amazonaws.com/urnotice/OrbitPage/User/Sumit/WallPost/ee167d15-52bb-4f4f-99fb-038f27eee1dc.jpeg",
      "ImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg",
      "LastName": "Sharma",
      "Gender": "NA",
      "_id": ​23041024,
      "_type": "vertex"
    }
  ]
}
],
"version": "2.5.0",
"queryTime": ​383.759295
}

I want to get post of all users whom i've followed and that in descending order according to PostTime of post Vertex. I've tried a lot but I'm not getting any approach how can I achieve this.


Solution

  • You are doing it right. Just first get all the vertices whom you are following and apply your get wall post logic on that all vertices, it will give you your desired result.

    g.v(2304).out('Follow').in('WallPost').sort {
        a, b -> b.PostedTime <=> a.PostedTime
    }.()[0..3]
    .transform {
        [
            postInfo: it,
            commentsInfo: it.in('Comment').sort {
                a, b -> b.PostedTime <=> a.PostedTime
            }.()[0..5].
            transform {
                [
                    commentInfo: it,
                    commentedBy: it.in('Created')
                ]
            },
            userInfo:it.in('Created')
        ]
    }
    

    So it will give you list of all post of your following and in descending order of PostedTime.