Querying titan graph for only indexed vertices without traversing complete graph - graph

I am trying to retrieve only indexed vertices in the graph
I used the below query
Iterator<Vertex> vertices = titanTransaction.query().has("name").vertices().iterator();
This query traverses to the complete graph and fetches the result, Can anyone suggest me a better way, The name vertex is indexed.
Thanks

You should be able to use the .has(key,value) method https://github.com/thinkaurelius/titan/blob/0.5.4/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/query/graph/GraphCentricQueryBuilder.java#L113
Iterator<Vertex> vertices = titanTransaction.query().has("name","john").vertices().iterator();
Should do the trick.

Have you tried:
graph.V('name','nameOfTargetVertex').next()
I think this should use your index on the attribute name, according to http://gremlindocs.com/#transform/v .

Related

How to get properties hasid/key with vertexes info in one gremlin query or Gremlin.Net

I try to get properties which has key or id in following query by Gremlin.Net, but vertex info(id and label) in VertexProperty is null in result.
g.V().Properties<VertexProperty>().HasKey(somekey).Promise(p => p.ToList())
So i try another way, but it's return class is Path, and i had to write an ugly code for type conversion.
g.V().Properties<VertexProperty>().HasKey(somekey).Path().By(__.ValueMap<object, object>(true))
Is there a better way to achieve this requirement
I think basically the only thing missing to get what you want is the Project() step.
In order to find all vertices that have a certain property key and then get their id, label, and then all information about that property, you can use this traversal:
g.V().
Has(someKey).
Project<object>("vertexId", "vertexLabel", "property").
By(T.Id).
By(T.Label).
By(__.Properties<object>(someKey).ElementMap<object>()).
Promise(t => t.ToList());
This returns a Dictionary where the keys are the arguments given to the Project step.
If you instead want to filter by a certain property id instead of a property key, then you can do it in a very similar way:
g.V().
Where(__.Properties<object>().HasId(propertyId)).
Project<object>("vertexId", "vertexLabel", "property").
By(T.Id).
By(T.Label).
By(__.Properties<object>(someKey).ElementMap<object>()).
Promise(t => t.ToList());
This filters in both cases first the vertices to only have vertices that have the properties we are looking for. That way, we can use the Project() step afterwards to get the desired data back.
ElementMap should give all information back about the properties that you want.
Note however that these traversals will most likely require a full graph scan in JanusGraph, meaning that it has to iterate over all vertices in your graph. The reason is that these traversals cannot use an index which would make them much more efficient. So, for larger graphs, the traversals will probably not be feasible.
If you had the vertex ids available instead of the property ids in the second traversal, then you could make the traversal a lot more efficient by replacing g.V().Where([...]) simply with g.V(id).

Tinkerpop Gremlin group by key and get latest

I am creating 2 users(uid=1 & uid=2) with 2 versions each.
g.addV('user1').property('uid',1).property('version',1)
.addV('user1').property('uid',1).property('version',2)
.addV('user1').property('uid',2).property('version',1)
.addV('user1').property('uid',2).property('version',2)
I want to get the latest version from each uid, I am using the uid as a groupBy key and getting the latest as shown
g.V().hasLabel('user1')
.group().by('uid').by(fold().order(Scope.local).by('version', Order.desc).unfold().limit(1)) //GraphTraversal<Vertex,Map<Object, Object>>
.flatmap(t -> t.get().values().iterator()) // convert to GraphTraversal<Vertex, Vertex>
//traverse out and get the path
.out('friend').path().by(elementMap())
Is the best approach for this requirement?
What would be the gremlin preferred way to convert the Map to a Vertex inside the flatmap rather than using the lambda? Suppose I want to add further steps after this.
Appreciate any help!
The group step has two modes. Without a label it acts as a barrier but with a label it acts as a side effect. You can have results flow through a group using your data as follows.
gremlin> g.V().group('x').by('uid').by(values('version').max())
==>v[42306]
==>v[42309]
==>v[42312]
==>v[42315]
==>v[42318]
gremlin> g.V().group('x').by('uid').by(values('version').max()).cap('x')
==>[1:2,2:2]
You can add more traversal steps of course before you decide what you want to do with the group. Such as:
g.V().group('x').by('uid').by(values('version').max())out()...

How to get the list of all incoming intermediate vertices between a source vertex and target vertex of specific label using Gremlin query?

The Gremlin query that I use to get the list of all outgoing vertices (with edge label “has”) from a selected vertex “P1” until it reaches the vertex with specific label “L3” is this:
g.V().has("id”,”P1”).repeat(out(“has”)).until(hasLabel(“L3”)).path().by("id")
As expected, the query above returns me the list of all intermediate nodes between selected vertex and target vertex labeled “L3”.
However, when using the same query (changing the ‘out’ to ‘in’) in opposite direction, i.e., to get the list of all incoming vertices from a selected vertex to the target vertex with specific label, i get a gremlin query error straight away.
Here is the query:
g.V().has("id”,”P3”).repeat(in(“has”)).until(hasLabel(“L1”)).path().by("id")
The error looks like this:
Failure in submitting query:
Error: Script compile error: Missing ')'
I don’t see any missing brackets in the query though and the only change between the queries for incoming or outgoing vertices i made is using ‘in’ instead of ‘out’.
In the official tinkerpop documentation (https://tinkerpop.apache.org/docs/3.2.9/reference/#_traversal_strategies_2), in traversal strategies, I cannot find any example with repeat(in()), only with repeat(out()). Is there a special query or method to get all the incoming vertices from a selected vertex until it reaches the vertex with a specific label?
I'm not sure if this is your problem or not, but I could see where you would get an error because "in" is a reserved word in Groovy so, you have to explicitly spawn it using the anonymous traversal class with: __.in(), therefore:
g.V().has("id","P3").repeat(__.in("has")).until(hasLabel("L1")).path().by("id")
This issue is documented in a number of places in the Reference Documentation, but perhaps you missed it (you referenced a fairly old version of the documentation as well) - described here in a NOTE in the Vertex Steps for example.

Gremlin bredcrumb query

I'm new to Gremlin and still learning.
I'd like to include the starting vertex in the results of the following:
g.V('leafNode').repeat(out()).emit()
This gives me a collection of vertexes starting from an arbitrary leaf node "upwards" to the root vertex. However this collection excludes the V('leafNode') vertex itself.
How do I include the V('leafNode') in this collection?
Thanks
-John
There are two places for the emit in this statement: either before the repeat or after. If it comes before the repeat, it will be performed before evaluating the next loop.
Source: http://tinkerpop.apache.org/docs/current/reference/#repeat-step
So the following should take care of what your request.
g.V('leafNode').emit().repeat(out())

Adding Multiple Unique Vertices

I want to write a single gremlin query that will create multiple vertices, but only does so if they are all unique. I know I can use the get or addV method by using coalesce step mentioned in the comments.
g.V().has('com.demo.test', '__type', 'Namespace').fold().coalesce(unfold(), addV('com.demo.test').property('__type', 'Namespace'))
This will hadd a single vertex only if it does not exist already. What if i want to do this same procedure for multiple edges and vertices all in a single query? My goal is that if one of the vertices/edges is not unique, none of them are created. However I understand that may not be possible so all answers are welcome.
Thanks
I found a possible solution. This works but there might be a better way to do it.
g.V().coalesce(
V().has(label,'Namespace61'),
addV('Namespace61')).
coalesce(
V().has(label,'Namespace76'),
addV('Namespace76')).
coalesce(
V().has(label,'Namespace74'),
addV('Namespace74')
).dedup()

Resources