question

sean.fennell_168661 avatar image
sean.fennell_168661 asked Erick Ramirez edited

Is it feasible to support natural sort when ordering?

We have some use cases where clients are supplying semantic version values as a string to a property. Then they want to sort these in the natural order to either populate a UI or procedurally select the highest version (for example).

Is it feasible to support natural sort option for these types of cases?

graph
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

bryn.cooke_101029 avatar image
bryn.cooke_101029 answered bryn.cooke_101029 published

The following example uses a self edge on person ordered by clustering key, but you could of course use a regular vertex at the end of your edge.

//Setup some schema
schema.vertexLabel('person').partitionBy('name', Text).property('age', Int).create()
schema.edgeLabel('address').from('person').to('person').partitionBy(OUT, 'name').clusterBy('since', Timestamp, Desc).clusterBy(IN,'name').property('street', Text).create()

//Add some data
g.addV('person').property('name', 'bob').as('bob').addE('address').from('bob').to('bob').property('since', Instant.now()).property('street', '1')
g.addV('person').property('name', 'bob').as('bob').addE('address').from('bob').to('bob').property('since', Instant.now()).property('street', '2')
g.addV('person').property('name', 'bob').as('bob').addE('address').from('bob').to('bob').property('since', Instant.now()).property('street', '3')

//Query for the latest address of bob
g.V().has('name', 'bob').outE('address').limit(1)

//Get all addresses most recent first.
g.V().has('name', 'bob').outE('address')

//Get all addresses oldest first.
g.V().has('name', 'bob').outE('address').order().by('since')

We will be pushing out a new version of graph in labs soon that will simplify the edge label declaration so that you don't have to supply all your primary key components to cluster by edge properties. For now you will need to specify your entire edge layout to get the desired behaviour.

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

bryn.cooke_101029 avatar image
bryn.cooke_101029 answered sean.fennell_168661 commented

It is, but the solution will depend on your use case:

If the number of versions that you expect will stay reasonably low and the property value is small then a collection of UDTs can be used. The UDT type would contain the version and value. The collection would currently have to be programmatically sorted on the client.

If the number of versions will grow in an unbounded fashion then an edge should be used.
The edge can be defined to use a clustering key (version) between the OUT and IN vertex PKs. This will allow you to get the edges ordered by version and also select the most recent edge.

Let us know if either of these sound like your use case, and we can supply you with an example.

1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

sean.fennell_168661 avatar image sean.fennell_168661 commented ·

The latter sounds more appropriate to my use case. Can you propose an example?



0 Likes 0 ·