Skip to content

Instantly share code, notes, and snippets.

@GabeStah
Last active March 28, 2019 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabeStah/5c1cec152b8fd14fb84103fb5bfc77a9 to your computer and use it in GitHub Desktop.
Save GabeStah/5c1cec152b8fd14fb84103fb5bfc77a9 to your computer and use it in GitHub Desktop.
Dgraph Child Node Query

Schema

The relevant schema:

<hashtag.hashtag>: string @index(exact, fulltext) @count .
<hashtag.indices>: [int] .
<tweet.hashtag>: uid @count .
<tweet.text>: string @index(exact, fulltext) @upsert @count .

Overview

I have a collection of Tweet nodes, which are associated with a handful of Hashtag nodes that represent parsed hashtags.

I'm trying to perform a search query that retrieves Tweet nodes in which the associated tweet.hashtag node contains a hashtag.hashtag edge value matching a given search.

For example, a Tweet node with a tweet.text value of "@Ladarius93 Persevering discrete algorithm #global #B2B" has a tweet.hashtag edge pointing to a Hashtag node with a hashtag.hashtag value of "global." Therefore, I'm trying to create a query that will allow me to retrieve the parent Tweet node when searching for the hashtag.hashtag value of "global."

Tweet Node Example

# query
{
  tweet(func: uid(0x56bc9)) {
    uid
    tweet.text
    tweet.hashtag {
      uid
      expand(_all_)
    }
  }
}

Result:

{
  "data": {
    "tweet": [
      {
        "uid": "0x56bc9",
        "tweet.text": "@Ladarius93 Persevering discrete algorithm #global #B2B",
        "tweet.hashtag": [
          {
            "uid": "0x56bc7",
            "hashtag.hashtag": "global",
            "hashtag.indices": [
              43,
              50
            ]
          },
          {
            "uid": "0x56bc8",
            "hashtag.hashtag": "B2B",
            "hashtag.indices": [
              51,
              55
            ]
          }
        ]
      }
    ]
  },
}

Issues

I've tried a combination of queries similar to the following, with no luck:

{
  tags as tag(func: anyoftext(hashtag.hashtag, "global")) {
    uid
    hashtag.hashtag
    hashtag.indices
  }
  
  filteredTweets(func: has(tweet.text)) {
    uid
    tweet.text
    tweet.hashtag @filter(uid(tags))
  }
}

Even though the tags variable result works and only returns the collection of Hashtag nodes in which hashtag.hashtag contains "global", the filteredTweets query returns the entire collection of Tweets (that is, anything that has tweet.text edge).

Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment