I have a graph with one start-node and two goal-vetices. Two paths lead to the first goal, another path to the second. I look for all paths to the goals vertices and collect the edge weights (sack(sum)). I add the sum of all paths leading to the same goal via group().by().
query so far:
g.withSack(1.0f).V('v0')
.repeat(
outE().sack(mult).by('weight')
.inV()
).until(hasLabel('goal'))
.group().by().by(sack())
.unfold()
.project('sack', 'map')
.by(select(values))
.by(select(keys).valueMap(true))
.order().by('sack', desc)
the result looks like this:
{'sack': 0.27999999999999997, 'map': {<T.id: 1>: 'v7', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
{'sack': 0.125, 'map': {<T.id: 1>: 'v3', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
now I want to also sort by date... However, using by('date', desc) results in an error:
"java.util.LinkedHashMap cannot be cast to java.lang.Comparable"
can I somehow access the date-value inside the map? or sort by date in some other way?
(fyi: by(sack(), desc) works just as well as by('sack', desc) )
data:
g.addV('start').property(id, 'v0')
.addV('road').property(id, 'v1')
.addV('road').property(id, 'v2')
.addV('goal').property(id, 'v3').property('date', '02-02-2022')
.addV('road').property(id, 'v4').
.addV('road').property(id, 'v5').
.addV('road').property(id, 'v6').
.addV('goal').property(id, 'v7').property('date', '22-02-2022')
.addE('link').property('weight', 0.4).from(V('v0')).to(V('v1'))
.addE('link').property('weight', 0.4).from(V('v1')).to(V('v2'))
.addE('link').property('weight', 0.4).from(V('v2')).to(V('v3'))
.addE('link').property('weight', 0.5).from(V('v0')).to(V('v5'))
.addE('link').property('weight', 0.5).from(V('v5')).to(V('v4'))
.addE('link').property('weight', 0.5).from(V('v4')).to(V('v3'))
.addE('link').property('weight', 0.7).from(V('v0')).to(V('v6'))
.addE('link').property('weight', 0.4).from(V('v6')).to(V('v7'))
(my code runs on Neptune with 'gremlin': {'version': 'tinkerpop-3.4.11'})
You just need to select the date
key from the map
map.
gremlin> g.withSack(1.0f).
......1> V('v0').
......2> repeat(outE().sack(mult).by('weight').inV()).
......3> until(hasLabel('goal')).
......4> group().by().by(sack()).
......5> unfold().
......6> project('sack', 'map').
......7> by(select(values)).
......8> by(select(keys).valueMap(true)).
......9> order().by('sack', desc).by(select('map').select('date'),desc)
==>[sack:0.280,map:[id:v7,label:goal,date:[22-02-2022]]]
==>[sack:0.1250,map:[id:v3,label:goal,date:[02-02-2022]]]
Note however, for the dates to sort properly (if you encode them as strings), you should use something close to ISO 8601 form, such as YYYY-MM-DD
or more concretely, 2022-02-22
. It might be more efficient to store actual dates, or epoch offset integers.
UPDATED based on comment thread 2022-02-23
OK, I think I figured out what you are seeing - I was testing using the Gremlin Console at the 3.5.2 level and it looks like you are using Amazon Neptune. Neptune is currently at the 3.4.11 level (but should be moving up to the 3.5.2 level fairly soon). Here is the modified query running at the TinkerPop 3.5.2 level.
gremlin> g.withSack(1.0f).
......1> V('v0').
......2> repeat(outE().sack(mult).by('weight').inV()).
......3> until(hasLabel('goal')).
......4> group().by().by(sack()).
......5> unfold().
......6> project('sack', 'map').
......7> by(select(values)).
......8> by(select(keys).valueMap(true)).
......9> order().
.....10> by(select('map').select('date').unfold(),asc)
==>[sack:0.1250,map:[id:v3,label:goal,date:[2022-02-02]]]
==>[sack:0.280,map:[id:v7,label:goal,date:[2022-02-22]]]