Query one node per same predicate value/UID

Hello,

I’m looking for a way to query a set of nodes N that all have a predicate P but I don’t want more than one node with the same value for P.

My use case is:

  • each node is a game
  • the predicate is a word
  • players pick 3 games but it has to be games on different words

If I use a first keyword, I get 3 games, but some can be on the same word:

{
  me(func: type(Game), first: 3) {
    uid
    word {
      uid
    }
  }
}

Possible response:

  "data": {
    "me": [
      {
        "uid": "0x6d1011",
        "word": {
          "uid": "0x6d0f88"
        }
      },
      {
        "uid": "0x6d101d",
        "word": {
          "uid": "0x6d0f88"
        }
      },
      {
        "uid": "0x6d1020",
        "word": {
          "uid": "0x6d0f89"
        }
      }
    ]
  }

Here we can see two games are on the same word 0x6d0f88.

Does dGraph language car filter out duplicate nodes based on a predicate value/UID?
Or do I have to fetch more results and then filter them programmatically?

Not sure if a get it.

{
  me(func: type(Game), first: 3) @recurse(depth: 3, loop: true) {
    uid
    word @filter(NOT eq(some, "value"))
  }
}

Thanks for your answer.
However I don’t have any predefined “value” to exclude in a filter.
I’d like to exclude previously found word UIDs.

In a traditional programmatic loop, the first iteration would be without constraints, the second would exclude the word UID of the first iteration, and the third iteration would exclude both first and second word UIDs.

For the record I found a way, but it requires a reverse index on word predicate :

{
  games as var(func: type(Game)) { // plus game filters
    uid
    words as word
  }
  me(func: uid(words), first: 3) {
    ~word @filter(uid(games)) (first: 1) {
      uid
      word { uid }
    }
  }
}

And now all returned games are on a different word:

  "data": {
    "me": [
      {
        "~word": [
          {
            "uid": "0xbef2f7", // game 1
            "word": {
              "uid": "0xbef275" // word
            }
          }
        ]
      },
      {
        "~word": [
          {
            "uid": "0xbef2ed", // game 2
            "word": {
              "uid": "0xbef276" // word
            }
          }
        ]
      },
      {
        "~word": [
          {
            "uid": "0xbef328", // game 3
            "word": {
              "uid": "0xbef277" // word
            }
          }
        ]
      }
    ]
  }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.