Gremlin query to order vertices with some locked to specific positions
I'm working with a product catalog in a graph database using Gremlin.
The graph structure includes:
1.
Product
vertices
2. Category
vertices
3. belongsTo
edges connecting products to categories. The edge can have an optional lockedPosition
property as integer.
I need to create a query that returns products in a specific order, where:
1. Some products are "locked" to specific positions (stored as a lockedPosition
property on the belongsTo
edge)
2. Other "unlocked" products should fill in around these fixed positions
For example, if I have 20 products in a category, and product A is locked to position 3 and product B to position 7, the result should return these products in those exact positions, with other products filling the remaining slots with whatever sorting order. (this "product pinning/locking" functionality is used by merchandising team to manually re-arrange products on a product listing page).
Can anyone suggest a Gremlin query to achieve this?
Also, maybe my data model is wrong for this use case, so I am also keen to accept other suggestions to represent this use case.
Thank you!Solution:Jump to solution
you can play tricks like this to move the 0 index to last place, but that still leaves the 7 one row off
```
gremlin> g.V().hasLabel("Category").
......1> inE("belongsTo").as('a').outV().
......2> path()....
7 Replies
Are you able to share a small test graph (a few
addV
and addE
steps) ? Having that makes writing tested answers a lot easier.Here is the sample graph:
Note, the edge
Product9 -belongsTo-> shoes
has property lockedPosition: 7
.
The edge Product10 -belongsTo-> shoes
has property lockedPosition: 3
.
A traversal like g.V().hasLabel("Category").in("belongsTo").values("name")
returns products with no specific order.
My goal is to create a traversal such that getting products in shoes
category would always result in a list, where 3rd and 7th positions would be always occupied by Product10
and Product9
respectively.
Alternatively, I am okay with accommodating a different data model for this use case, if it makes for simpler or more efficient traversals.I don't think there is that sort of fine-grained control over traversal iteration order or collection order.
Do you have any ideas @spmallette ?
i missed this one for some reason. another interesting question from @Max
wonder if this could be done.....
i sense it's going to end up being one those things where it makes more sense to just handle this logic on the application side because the Gremlin to do it will be impenetrable to read.
cant quite get it...
i thought i could use
index()
to help with the ordering, but i guess as you go to "insert" a lockedPosition value in the right place technically disturbs the index. as a result, i sense this way will only work if you had one "lockedPosition" set, not multiple. anyway, for fun:
that bit of Gremlin might be a bit overdone, but since it's not working quite right i see no reason to clean up. still might be possible to figure out though for academic purposes.I wonder if the solution to a similar situation I posted in the tinkerpop-tips area could be used here as well? https://discord.com/channels/838910279550238720/1156920912206110834/1254910222837874759
This is not quite there - but it is close
I do think considering data model changes, or just doing this in code within the application (if possible) would be simpler.
This is almost there, other than being zero based on the index and the 7 still being one row off
Solution
you can play tricks like this to move the 0 index to last place, but that still leaves the 7 one row off
Thank you! It was useful, I didn't know you could use
by
modulator for order
like this.