Richard
Richard

Reputation: 65600

Basic Gremlin: look up node properties?

Beginner Gremlin question. I'd like to retrieve a node from the graph using a property name, and then print all its attached properties.

This is what I'm trying:

println g.v(20020000001901003)

That's giving me null. When I try this:

println g.idx('mygraph')[[id:20020000001901003]]

the output is [StartPipe].

How can I get from StartPipe to the node's properties?

Thanks!

Upvotes: 3

Views: 10910

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

Here's some examples from the Gremlin terminal using the toy graph and TinkerPop 2.x (3.x instructions are further below). The following console session shows how to create a key index and search on it.

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.createKeyIndex("name",Vertex.class)
==>null
gremlin> g.V("name","marko").name
==>marko
gremlin> g.V("name","marko").map
==>{age=29, name=marko}

You should note that the reason g.v(20020000001901003) returns null for you is because that function tries to look up the vertex based on the unique identifier for the element in the graph, and not your assigned identifier (very few graphs support user assignment of the id...they generally generate their own). Consider the following where I'm using the assigned identifier to access the vertex:

gremlin> g.V("name","marko")
==>v[1]
gremlin> g.v(1).map
==>{age=29, name=marko}

If you have created a manual index, then you would use the g.idx syntax that you are referencing. Here's an example:

gremlin> idx = g.createIndex("my-index",Vertex.class)
==>index[my-index:Vertex]
gremlin> idx.put("id", 1000, g.v(1))
==>null
gremlin> g.idx("my-index")[[id:1000]]
==>v[1]

I assume that you are not using the Gremlin terminal and as such you would need to iterate that start pipe. You might do something like sending it to a List:

gremlin> x=[];g.idx("my-index")[[id:1000]].fill(x)
==>v[1]
gremlin> x.size()
==>1

In TinkerPop 3.x there is no TinkerPop abstraction over indices. You must use the method for index creation prescribed by the underlying graph database. For example, in neo4j you would use some Cypher syntax. With TinkerGraph, there is just a createIndex() method. You can see it's usage as follows:

gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> graph.createIndex('name',Vertex.class)
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('name','stephen')
==>v[0]
gremlin> g.addV('name','steve')
==>v[2]
gremlin> g.V().has('name','stephen')
==>v[0]

Note that when doing a lookup with that last line above, there is no explicit syntax to use from Gremlin's perspective to use the index on "name". TinkerGraph automatically detects the use of has() and that the key is "name" and then it uses the index. If it didn't find an index for "name" it would do a full scan of the vertices to find "stephen". All TinkerPop implementations will have similar strategies for doing those kinds of index lookups.

Upvotes: 6

Related Questions