Gremlin query language - how to limit/filter path or start from specific verticle in chain?

106 views Asked by At

I am new to the graph databases and Gremlin. What I am trying to achieve is to ask a simple question to the database. Provided id of the Entity and id of the File, I want to find all other entites who have access to this file via the Share and return the result with the property "type" of the share. Here is the graph itself:Simple graph

What I tried:

g.V("Enity:1").out("Created").hasLabel("Share").where(out("Shared").hasId("File:1")).in("Access").path()

This returns me:

[
  {
    //Omitted empty aray
    "objects": [
      {
        "id": "dseg:/Entity/1",
        "label": "Entity",
        "type": "vertex",
        "properties": {}
      },
      {
        "id": "dseg:/Share/1",
        "label": "Share",
        "type": "vertex",
        "properties": {}
      },
      {
        "id": "dseg:/Entity/1",
        "label": "Entity",
        "type": "vertex",
        "properties": {}
      }
    ]
  },
  {
    //Omitted empty aray
    "objects": [
      {
        "id": "dseg:/Entity/1",
        "label": "Entity",
        "type": "vertex",
        "properties": {}
      },
      {
        "id": "dseg:/Share/1",
        "label": "Share",
        "type": "vertex",
        "properties": {}
      },
      {
        "id": "dseg:/Entity/3",
        "label": "Entity",
        "type": "vertex",
        "properties": {}
      }
    ]
  }
]

It shows the path taken from the entry point(Entity:1) to the target(s) Entity 2 and 3

My question with technical details: Is there a way to start gathering path() after Entity:1 or intermediate verticle I chose, so that in result I will get only Share + Entity who has Access to it?

1

There are 1 answers

0
PrashantUpadhyay On BEST ANSWER

You just need to tell path step where to start from. You can do something like this:

gremlin> g.V("entity:1").
......1>   out("Created").
......2>   hasLabel("Share").
......3>   where(out("Shared").hasId("File:1")).as('a').
......4>   in("Access").
......5>   path().from('a').by('Type').by()
==>[RO,v[entity:2]]
==>[RO,v[entity:3]]

Another way to do this is :

gremlin> g.V().
......1>   hasLabel('Share').
......2>   where(out('Shared').hasId('File:1')).
......3>   where(__.in('Created').hasId('entity:1')).
......4>   in('Access').
......5>   path().by('Type').by()
==>[RO,v[entity:2]]
==>[RO,v[entity:3]]