Order by facets not working

I have added the following data to my graph

{
  set{
  _:apple fruitName "apple" (size=10) .
  _:lemon fruitName "lemon" (size=5) .
  _:melon fruitName "melon" (size=20) .
}
}

Then query this (order fruits by size)

{
  fruits(func: has(fruitName)) {
    fruitName@facets (orderasc:size)
  }
}

Response generated, does not sort by size. Retains the order of insertion. Any reason why this is happening?

"data": {
    "fruits": [
      {
        "fruitName|size": 10,
        "fruitName": "apple"
      },
      {
        "fruitName|size": 5,
        "fruitName": "lemon"
      },
      {
        "fruitName|size": 20,
        "fruitName": "melon"
      }
    ]
  }

Looks like ordering works only on UID facets and not scalar facets.

  • (My mistake, It’s UID relation)

https://docs.dgraph.io/query-language#sorting-using-facets

hmmm, I get it. Actually sorting by facets could only be by UID. This is the main proposal of the Facets. Add extra information to the linked Edges.

You are creating three Nodes without relationships and categorizing by size. And you’re doing an open query with “has” by the predicate “fruitName”. That way you would dig the entire DB to find possible thousands of “fruitName”. All “loose” unrelated, in the “wide open”.

This that wants to make seems to me that really is not supported even because the facets function was not intended for this. Without relation it does not make sense to order by a “lonely” predicate.

The solution in your case would be to create a “Root” Node that relates to these fruits.

3 posts were split to a new topic: Sorting Facets