Frage

I had like to filter vertex names ( overlap ) by comparing one property of each edge's .

Suppose I have a simple dataset as :

A  --> 100  --> B
A  --> 200  --> C
A  --> 100  --> D

See in the above I want to show common vertices based on edge property (100, 200 etc) like :

A 100 B
A 100 D

This is the Gremlin code I tried :

g.V.outE('give').filter{it.amt.next() == it.amt.next()}

also not able to output using has clause ? It doesn't return any results. What have I missed ?

War es hilfreich?

Lösung

One way would be to use groupBy operation (a similar recommendation was made by @abhi in the comments to use groupCount):

gremlin> g = new TinkerGraph()
==>tinkergraph[vertices:0 edges:0]
gremlin> a = g.addVertex("A") 
==>v[A]
gremlin> b = g.addVertex("B") 
==>v[B]
gremlin> c = g.addVertex("C")              
==>v[C]
gremlin> d = g.addVertex("D")                
==>v[D]
gremlin> g.addEdge(a, b, 'give', [amt:100])
==>e[0][A-give->B]
gremlin> g.addEdge(a, c, 'give', [amt:200])
==>e[1][A-give->C]
gremlin> g.addEdge(a, d, 'give', [amt:100])
==>e[2][A-give->D]
gremlin> g.V.outE('give').groupBy{it.amt}{[it.outV.next(), it.inV.next()]}.cap.next()
==>100=[[v[A], v[D]], [v[A], v[B]]]
==>200=[[v[A], v[C]]]

To limit the results to just those that have more than one match you can just post process the map with groovy (in this case with findAll):

gremlin> m=[:];g.V.outE('give').groupBy(m){it.amt}{[it.outV.next(), it.inV.next()]}        
==>e[2][A-give->D]
==>e[1][A-give->C]
==>e[0][A-give->B]
gremlin> m
==>100=[[v[A], v[D]], [v[A], v[B]]]
==>200=[[v[A], v[C]]]
gremlin> m.findAll{it.value.size()>1}
==>100=[[v[A], v[D]], [v[A], v[B]]]

Andere Tipps

It is always better to ask specific questions with real world examples than generalizing the problems. Based upon my understanding, below is the Cypher notation. I am giving an example in Cypher that I think you can convert to Gremlin. If you doesn't want it then comment and I will delete this response.

Setup

CREATE (A { name: 'A' })-[:GAVE { amount: 100 }]->(B { name: 'B' }) 
CREATE A-[:GAVE { amount: 200 }]->(C { name: 'C' }) 
CREATE A-[:GAVE { amount: 100 }]->(D { name: 'D' })

Query

Returns all the Nodes (vertices) where the amount is 100.

MATCH n1-[gave:GAVE]->n2 
WHERE gave.amount=100 
RETURN n1.name, gave.amount, n2.name

You can try this in the console here.

Update your question with real world example and better setup the test data in neo4j.console.org and then Share it by clicking the Share button on top of the console.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top