Search code examples
javagraph-databasesgremlinamazon-neptune

How does searching for a vertex happen in graph databases using gremlin (Apache TinkerPop)?


I am working on modeling my data such as ->

data: [
 {
  id:"123",
  type:"a",
  attributes: [...]
 },
 {entity 2 ...},
 {entity 3 ...},
 ...
]

Is there a gremlin query I can use to fetch/get the vertex by type and id instead of just id? If not, would I have to traverse and search, if so what would the performance look like?


Solution

  • Type of entity in Gremlin is called Label.

    To get a vertex by id and verify it has specific label you can run the query:

    g.V('123').hasLabel('a').next()
    

    If type is just a regular property (attribute) you can run:

    g.V('123').has('type', 'a').next()
    

    Performance depends on implementation, but getting vertex by id should be O(1) in any case.