Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting by multiple facets #3638

Closed
KevOrr opened this issue Jul 8, 2019 · 4 comments · Fixed by #4579
Closed

Sorting by multiple facets #3638

KevOrr opened this issue Jul 8, 2019 · 4 comments · Fixed by #4579
Assignees
Labels
area/facets Issues related to face handling, querying, etc. exp/beginner Something most people could solve. kind/enhancement Something could be better. status/accepted We accept to investigate/work on it.
Milestone

Comments

@KevOrr
Copy link

KevOrr commented Jul 8, 2019

Experience Report

Note: Feature requests are judged based on user experience and modeled on Go Experience Reports. These reports should focus on the problems: they should not focus on and need not propose solutions.

What you wanted to do

You can use orderasc to sort by multiple predicates, which will combine their orderings using lexicographic ordering:

{
  asdf(func: has(DOB), orderasc: DOB, orderasc: name) {
    uid
    DOB
    name
  }
}

It would be nice if we could lexicographically sort by multiple facets in the same way:

{
  asdf(func: has(edge)) {
    uid
    edge @facets(kind, index, orderasc: kind, orderasc: index) {
      uid
    }
  }
}

What you actually did

Sort by one facet, and then sort by the other on the client side, or leave unsorted and handle all sorting client-side.

Why that wasn't great, with examples

In complex, deeply nested queries, it can be cumbersome to post-process results. It divides the intended query into two steps: the actual query, and then a sorting phase. This is trickier to maintain. Besides, graphql(+-) already lends itself nicely to things such as sorting.

Any external references to support your case

https://discuss.dgraph.io/t/sorting-by-multiple-facets/4670

@MichelDiz MichelDiz added the area/facets Issues related to face handling, querying, etc. label Aug 15, 2019
@campoy campoy added exp/beginner Something most people could solve. kind/enhancement Something could be better. status/accepted We accept to investigate/work on it. labels Sep 15, 2019
@campoy campoy added this to the Dgraph v1.2 milestone Sep 15, 2019
@campoy
Copy link
Contributor

campoy commented Sep 15, 2019

This seems like a pretty straight forward request that makes total sense.

We'll consider it for the roadmap in v1.2.

Thanks for the report, @KevOrr !

@MichelDiz
Copy link
Contributor

MichelDiz commented Oct 31, 2019

Just a kind of "note" update on this:

After rereading this issue. I noticed that we could try another way*. However, the problem continues even after trying to reproduce. In fact the result is even worse because val(K) is in the totally wrong order.

Query

{
  F as var(func: eq(name, "Carol Lelvits")) {
    var : friend @facets(close, kind, index, K as since)
  }
    
  q(func: uid(F)) {
    uid
    friend(orderasc: val(K)) @facets(close, kind, index, since) {
      uid
      name
      val(K) # Note that the value in this variable comes in order, but the nodes themselves do not.
    }
  }
}

Result

{
  "data": {
    "q": [
      {
        "uid": "0x2711",
        "friend": [
          {
            "uid": "0x2715",
            "name": "Daryl",
            "val(K)": "2001-01-01T00:00:00Z",
            "friend|close": "true",
            "friend|index": "1",
            "friend|kind": "Childhood",
            "friend|since": "2002-01-01T00:00:00Z"
          },
          {
            "uid": "0x2712",
            "name": "Lily",
            "val(K)": "2002-01-01T00:00:00Z",
            "friend|close": "false",
            "friend|index": "2",
            "friend|kind": "School",
            "friend|since": "2011-01-01T00:00:00Z"
          },
          {
            "uid": "0x2713",
            "name": "Lucas",
            "val(K)": "2011-01-01T00:00:00Z",
            "friend|close": "false",
            "friend|index": "3",
            "friend|kind": "School",
            "friend|since": "2011-01-01T00:00:00Z"
          },
          {
            "uid": "0x2714",
            "name": "Junior",
            "val(K)": "2011-01-01T00:00:00Z",
            "friend|close": "true",
            "friend|index": "0",
            "friend|kind": "Childhood",
            "friend|since": "2001-01-01T00:00:00Z"
          }
        ]
      }
    ]
  }
}

Dataset

{
	"set": [
		{
			"name": "Carol Lelvits",
			"name|initial": "C",
			"friend": [{
					"name": "Daryl",
					"friend|close": "true",
					"friend|index": "0",
					"friend|kind": "Childhood",
					"friend|since": "2001"
				},
				{
					"name": "Lily",
					"friend|close": "true",
					"friend|index": "1",
					"friend|kind": "Childhood",
					"friend|since": "2002"
				},
				{
					"name": "Lucas",
					"friend|close": "false",
					"friend|index": "2",
					"friend|kind": "School",
					"friend|since": "2011"
				},
				{
					"name": "Junior",
					"friend|close": "false",
					"friend|index": "3",
					"friend|kind": "School",
					"friend|since": "2011"
				}

			]
		}

	]
}

@campoy
Copy link
Contributor

campoy commented Dec 6, 2019

Another user requested this via https://discuss.dgraph.io/t/do-facets-support-multi-sorting/5587/4

@ashish-goswami ashish-goswami self-assigned this Jan 16, 2020
ashish-goswami added a commit that referenced this issue Jan 20, 2020
Fixes: #3638

Currently, we can specify ordering only on single facet while retrieving facets on uid predicates.
This PR adds support for specifying ordering on more than one facets.
@ashish-goswami
Copy link
Contributor

Hey @KevOrr, we have merged PR for this change in master. Please let us know you feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/facets Issues related to face handling, querying, etc. exp/beginner Something most people could solve. kind/enhancement Something could be better. status/accepted We accept to investigate/work on it.
Development

Successfully merging a pull request may close this issue.

4 participants