Hello dear gremlin jedi,
I have a bunch of nodes with different labels in my graph:
g.addV("book").property(:genre, "horror").as("b1")
.addV("book").property(:genre, "biography").as("b2")
.addV("book").property(:genre, "sci-fi").as("b3")
.addV("movie").property(:genre, "biography").as("m1")
.addV("movie").property(:genre, "sci-fi").as("m1")
.addV("movie").property(:genre, "horror").as("m3").iterate
And I'd like to sort them by the genre property providing weights for each value from the client, something like:
WEIGHTS = {
"horror": 3,
"biography": 2,
"sci-fi": 1
}
So the result in this case should be something like
[b3, m1, b2, m1, b1, m3]
In case if a vertex has a genre not mentioned in the weights it should not cause an error, such vertices may be simply ignored or put in the end of results.
The weights might change from query to query, so I can't store them in the graph.
My idea is to use the withSideEffect
configuration step to pass the weights to the server and then somehow use it to sort the vertices:
g.withSideEffect("weights", WEIGHTS)
.V().order().by(__.select("weights").select(__.values(:genre)))
.toList()
But apparently it's a wrong way because it results with an error:
This traversal parent does not support the replacement of local traversals: org.apache.tinkerpop.gremlin.process.traversal.step.map.TraversalSelectStep
What would be the right way to achieve my goal?
Thank you!
PS: if it's important, I use my own gremlin language variant written in Ruby, but it's semantics more or less follows semantics of the official python client.
You were actually quite close with your original attempt in the question, just missing one more select
gremlin> g.withSideEffect("weights", ["horror":3,"sci-fi":1,"biography":2]).
......1> V().as('a').
......2> order().
......3> by(select('weights').select(select('a').values('genre'))).
......4> elementMap()
==>[id:42780,label:book,genre:sci-fi]
==>[id:42784,label:movie,genre:sci-fi]
==>[id:42778,label:book,genre:biography]
==>[id:42782,label:movie,genre:biography]
==>[id:42776,label:book,genre:horror]
==>[id:42786,label:movie,genre:horror]
By the way I would be very interested to hear more about your Ruby client, especially if it is available as open source.