Facets reply field layout

Hello,
I am finding working with facets a bit cumbersome. Consider this example: the IT department of a company tracks which kind of devices are assigned to who (laptop, mobile, keys,…); a person uses multiple devices and an application tracks which devices the person is currently using and which one it used before (using facets).

to get the list of devices for a person I will write something like this:

query {
	q(func:eq(name,"John Smith")) { 
   	uid
  	hasDevice @facets(until)
    {
      uid
    }
  }
}

and the reply would be something like

{
  "data": {
    "q": [
      {
        "uid": "0x2b02",
        "hasDevice": [
          {
            "uid": "0x2b06"
          },
          {
            "uid": "0x2b07"
          },
          {
            "uid": "0x2b09"
          },
          {
            "uid": "0x2b11"
          },
          {
            "uid": "0x2b13"
          },
          {
            "uid": "0x2b35"
          },
          {
            "uid": "0x2b5a"
          },
          {
            "uid": "0x53489"
          },
          {
            "uid": "0x534ae"
          },
          {
            "uid": "0x534b0"
          },
          {
            "uid": "0x5577b"
          },
          {
            "uid": "0x1b5129"
          },
          {
            "uid": "0x1b5236"
          }
        ],
        "hasDevice|until": {
          "2": "2020-01-31T00:00:00Z",
          "8": "2020-05-25T00:00:00Z"
        }
      }
    ]
  },
  ....
}

From a user standpoint the structure of the reply for the facets looks baroque and it requires custom code to assemble the results.

For example something like the following would be easier to deal with:

{
  "data": {
    "q": [
      {
        "uid": "0x2b02",
        "hasDevice": [
          {
            "uid": "0x2b06"
          },
          {
            "uid": "0x2b07"
          },
          {
            "uid": "0x2b09"
            "@until": "2020-01-31T00:00:00Z"
          },
          {
            "uid": "0x2b11"
          },
          {
            "uid": "0x2b13"
          },
          {
            "uid": "0x2b35"
          },
          {
            "uid": "0x2b5a"
          },
          {
            "uid": "0x53489"
          },
          {
            "uid": "0x534ae"
            "@until": "2020-05-25T00:00:00Z"
          },
          {
            "uid": "0x534b0"
          },
          {
            "uid": "0x5577b"
          },
          {
            "uid": "0x1b5129"
          },
          {
            "uid": "0x1b5236"
          }
        ]
      }
    ]
  },
  ....
}

Would you suggest alternative approach for the above scenario?

Thanks,
Andrea

Hi @noandrea,

I am not sure of the exact schema that you are using. It would be helpful if you could share that.

Consider the following mutation:

{
	set{
    _:a <name> "alice" .
    _:a <hasDevice> _:b (until=2010-01-02T15:04:05) .
    _:a <hasDevice> _:c (until=2020-01-02T15:04:05 ) .
    _:b <name> "iphone" .
    _:b <dgraph.type> "phone" .
    _:c <name> "samsung" .
    _:c <dgraph.type> "phone" .
  }
}

Consider the following query:

{
  q(func: eq(name, "alice")) {
    uid
    hasDevice @facets(until){
      uid
      name
    }
  }
}

The response is:

    "q": [
      {
        "uid": "0xa",
        "hasDevice": [
          {
            "uid": "0x9",
            "name": "samsung",
            "hasDevice|until": "2020-01-02T15:04:05Z"
          },
          {
            "uid": "0xb",
            "name": "iphone",
            "hasDevice|until": "2010-01-02T15:04:05Z"
          }
        ]
      }
    ]

Hi @noandrea,

We have recently updated the response for facet query on master and the new response format is similar to the way you are suggesting. Take a look at this pull request for details.

I think you can use build Dgraph from master branch to use this change.

Hope this helps.

@ashishgoswami FYI

3 Likes

Hi @noandrea, as @Rahul mentioned, we have changed facets format recently to have better user experience. New format can be seen from v20.07.
To know more about changes, you can follow this RFC Facets format in mutation requests and query responses

2 Likes