Add incoming and outgoing vertices to Gremlin query

60 Views Asked by At

I have a query which needs to satisfy some criteria which works well at the moment. The problem is that I'm also trying to include its incoming and outgoing edges to the response as below.

      {
        "id": "ba872479-90be-4478-b54b-00ea6540fb23",
        "label": "link",
        "type": "vertex",
        "properties": {
         ListOfIncomingVerticesForThisObject: [],
         ListOfOutgoingVerticesForThisObject: [],
           
          "state": [
            {
              "id": "71d5c4e5-e89f-424b-9894-af1177ea2294",
              "value": 1
            }
          ],
          "title": [
            {
              "id": "024fde3e-70ce-4d15-8ba1-92e488ffa36e",
              "value": "Some title"
            }
          ],
          "modified": [
            {
              "id": "6cb6e49f-16a8-4308-af41-8b68b14d3d97",
              "value": "2021-02-01"
            }
          ]
        }
      }

So far this is my query (I have omitted some info for obvious reasons)

g.V().
  hasLabel('link').
  has("created", between("2020-03-15", "2022-03-15")).
  has("path", TextP.containing("sometext")).
  has('type', within('some types')).
  has('domain', within('domain.com')).
  fold().as('files', 'count').
  select('files', 'count').by(range(local, 0, 1000)).by(count(local))

How can I add the incoming and outgoing vertices to this request?

1

There are 1 best solutions below

0
Kelvin Lawrence On

I'm not sure if CosmosDB supports all these Gremlin steps, but trying to keep with the query formulation you already have, using some sample data from a graph I had handy, the desired result can be achieved using:

g.V('752','753').
  fold().as('v','count').
  select('v','count').
    by(unfold().
       project('id','out','in').
         by(id).
         by(out().fold()).
         by(in().fold()).fold()).
    by(count(local)).
  unfold()

which gives

1   {'v': [{'id': '752', 'out': [v[214], v[105]], 'in': [v[214], v[105], v[3612], v[3746]]}, {'id': '753', 'out': [v[214], v[105]], 'in': [v[214], v[105], v[3612], v[3746]]}]}
2   {'count': 2}

The unfold at the end is just to aid readability a little. In general I would look for other query patterns that avoid doing the fold so early in the query. Perhaps using a group step or even two nested groups.