0

For example, let's say your database contains a bunch of People{ name } objects, and you want to find everyone whose name starts with the letter "H" or "Mi".

sdfsdf
  • 5,052
  • 9
  • 42
  • 75

1 Answers1

0

You should use Regex https://docs.dgraph.io/query-language/#regular-expressions But, regex accepts at least 3 characters.

Other way you could do is using "has" function and ordering by the predicate. https://docs.dgraph.io/query-language/#has

{
  me(func: has(name), first: 50, orderasc: name) {
    name
  }
}

But with 'Has' function you don't have full control. It will follow an alphabetical order.

One way to have this level of control is to create a pseudo-indexing structure. Every time you add a user you relate him to a tree that will play the role of indexing. And through this tree you can count and traverse the edge.

e.g:

Let's assume you know the UID of your tree (or that you use Upser Block).

_:NewUser <name> "Lucas" .
_:NewUser <pred1> "some1" .
_:NewUser <pred2> "some2" .

# Now we gonna relate Lucas to our tree and <0x33f> is the uid for the letter L of "Lucas"

 <0x33f> <firstCharacter> _:NewUser . #This is a reverse record (you don't need to use @reverse tho)

The same example using Upsert Block:

upsert {
  query {
    v as var(func: type(myTree)) @filter(eq(letter, "L"))
  }

  mutation {
    set {
      _:NewUser <name> "Lucas" .
      _:NewUser <pred1> "some1" .
      _:NewUser <pred2> "some2" .
      uid(v) <firstCharacter> _:NewUser .
    }
  }
}

A basic search in the tree would be like:

{
  q(func: type(myTree)) @filter(eq(letter, "L")) {
    letter
    firstCharacter { name pred1 pred2 }
  }
}

Or

{
  q(func: type(myTree)) @filter(eq(letter, "L")) {
    letter
    count(firstCharacter)
  }
}

Cheers.