Gremlin query: Find all the related vertices till end which match with edge properties - gremlin

I need to start with one vertex and find all the related vertices till end. Criteria is to match any one of the edge properties(attributes) in the edge inV vertex. If edge attribute ‘value’ doesn’t match inV vertex ‘attribute’ name I should skip the vertex. Attribute value of edge is propagated as Attribute name in the inV vertex
Am using below query, however this gives me json output of parent node, next node and edges between. With the output am writing logic to pick only next attributes which match with the edge attributes. If the matching of attributes can be done with gremlin query, that would be great
var graphQuery = "g.V().has('sPath', '/Assisted/CSurvey/CSurvey.ss').as('ParentStream').outE.inV().as('Edges').map(select('Edges').inV().fold()).as ('NextStream').select('ParentStream', 'NextStream','Edges')";
In below/attached image. I need to get vertex1 and vertex2 and skip vertex3 as there are no attributes matching with edge
image link

Use graph traversal and filter
Example in Scala:
graph.traversal().V().has().bothE('sPath').filter{it:Edge =>
(it.property('sPath').value() == '/Assisted/CSurvey/CSurvey.ss')}.toList()
Hope this helps

Related

Why can't I MATCH (v:<tag>)-[e:<edge>]-(v2:<tag>) RETURN v LIMIT 10 in the NebulaGraph database

The Nebula Graph docs say that "When traversing all vertices of the specified Tag or edge of the specified Edge Type, such as MATCH (v:player) RETURN v LIMIT N, there is no need to create an index, but you need to use LIMIT to limit the number of output results." But when I run the statement in the preceding screenshot, it told me that I did not have a limit number, which I did.
What is the correct way to RETURN v without creating indexes?
I met the same issue before. Actually, when you specify both a tag and an edge for a query simultaneously, you need to create an index for the tag or the edge first.
Create an index for the tag company first and then try to execute it again.

How to properly use MATCH inside UNWIND for a Nebula query

I’m currently working with the Nebula graph database for the first time and I’m running into some issues with a query. In terms of the schema, I have “Person” nodes, which have a “name” property, as well as Location nodes also with a name property. These node types can be connected by a relationship edge, called HAS_LIVED (to signify whether a person has lived in a certain location). Now for the query, I have a list of names (strings). The query looks like:
UNWIND [“Anna”, “Emma”, “Zach”] AS n
MATCH (p:Person {name: n})-[:HAS_LIVED]->(loc)
RETURN loc.Location.name
This should return a list of three places, i.e. [“London”, “Paris”, “Berlin”]. However, I am getting nothing as a result from the query. When I get rid of the UNWIND and write three separate MATCH queries with each name, it works individually. Not sure why.
Try this instead. It is using "where" clause.
UNWIND [“Anna”, “Emma”, “Zach”] AS n
MATCH (p:Person)-[:HAS_LIVED]->(loc)
where p.name = n
RETURN loc.Location.name

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.

Get path lengths for every relationship neo4j

So I have a graph that looks like this(starting from the rightmost side) with relationships that have a unique number attribute called Isnad. I want to write a query to get the length of every Isnad from the start node to the end node but I can't figure it out. I don't know how to traverse every path for every Isnad separately. Any help?
I don't know if it is the most elegant and solution, but I think it works. First, I'm getting all unique Isnad values of relationships outgoing from the rightmost side node using an identifier. Then I'm using a variable-length pattern matching where all relationships have the same value for Isnad property. Then the Isnad value and the path length are returned.
match ({id:'unique-identifier-of-rightmost-side-node'})-[r]->()
with distinct r.Isnad as Isnad
match p = ()-[*{Isnad : Isnad}]->()
return Isnad, length(p) as Length

Gremlin, join vertices of same type and add edge when the properties empNo and mgrno matches

I have vertices with properties like
vertex("empNo","age","Date","mgrNo")
a(101,20,'dd-mm-yy',0)
b(102,22,'dd-mm-yy',101)
Since mgrNo of b matches with empNo of a ie, a is the manager of b.
I have to add an edge between a and b.
Please tell me how to do this in gremlin .
I assume that you want to iterate all vertices. You could do it with a sideEffect pretty easily:
g.V.has("mgrNo",neq,0).sideEffect{
g.V.has("empNo",it.mgrNo).next().addEdge("manages",it)
}
Note that if you are using a graph that supports transactions you will need to commit() your changes to persist them.

Resources