Graph database: dgraph basic query

Keywords: Database Attribute curl less

Basic Request Form and Data Preparation

http mode

Alpha server port

  1. / alter is used to modify table structure including deleting database
  2. / query user query
  3. / mutate adds or modifies data
  4. / commit commit transaction

alter example:

Add name (string type field). Used for subsequent queries. If there is no index, it is not conducive to query.

curl -X POST http://127.0.0.1:8080/alter -d 'name: string @index(term) .'
# Return {"code": "Success", "message", "Done"}
# Delete full library fields
curl -X POST http://127.0.0.1:8080/alter -d 'name: string @index(term) .'
# When deleting the whole library, do it first.
curl -X POST http://127.0.0.1:8080/alter -d '{"drop_all": true}'

Test data preparation

# Add a database structure for later searches 
curl -X POST http://127.0.0.1:8080/alter -d $'
name: string @index(term) @lang .
age: int @index(int) .
friend: uid @count .
'
# - H "X-Dgraph-CommitNow: true" represents transaction autocommit
# This section focuses on queries, specific transactions are introduced later. In general, mutate is submitted with commit
curl -X POST http://127.0.0.1:8080/mutate -H "X-Dgraph-CommitNow: true" -d $'
{
  set {
    _:michael <name> "Michael" .
    _:michael <age> "39" .
    _:michael <friend> _:amit .
    _:michael <friend> _:sarah .
    _:michael <friend> _:sang .
    _:michael <friend> _:catalina .
    _:michael <friend> _:artyom .
    _:michael <owns_pet> _:rammy .

    _:amit <name> "अमित"@hi .
    _:amit <name> "অমিত"@bn .
    _:amit <name> "Amit"@en .
    _:amit <age> "35" .
    _:amit <friend> _:michael .
    _:amit <friend> _:sang .
    _:amit <friend> _:artyom .

    _:luke <name> "Luke"@en .
    _:luke <name> "Łukasz"@pl .
    _:luke <age> "77" .

    _:artyom <name> "Артём"@ru .
    _:artyom <name> "Artyom"@en .
    _:artyom <age> "35" .

    _:sarah <name> "Sarah" .
    _:sarah <age> "55" .

    _:sang <name> "상현"@ko .
    _:sang <name> "Sang Hyun"@en .
    _:sang <age> "24" .
    _:sang <friend> _:amit .
    _:sang <friend> _:catalina .
    _:sang <friend> _:hyung .
    _:sang <owns_pet> _:goldie .

    _:hyung <name> "형신"@ko .
    _:hyung <name> "Hyung Sin"@en .
    _:hyung <friend> _:sang .

    _:catalina <name> "Catalina" .
    _:catalina <age> "19" .
    _:catalina <friend> _:sang .
    _:catalina <owns_pet> _:perro .

    _:rammy <name> "Rammy the sheep" .

    _:goldie <name> "Goldie" .

    _:perro <name> "Perro" .
  }
}
'

The format is basically
Uid < attribute name > value.
Uid < relation > relation uid.

Equivalent to json

{
    "set": [{
            "uid": "_:michael",
            "name": "Michael",
            "age": 39,
            "owns_pet": {
                "uid": "_:rammy",
                "name": "Rammy the sheep"
            },
            "friend": [{
                    "uid": "_:amit"
                },
                {
                    "uid": "_:sarah"
                },
                {
                    "uid": "_:sang"
                },
                {
                    "uid": "_:catalina"
                },
                {
                    "uid": "_:artyom"
                }
            ]
        },
        {
            "uid": "_:amit",
            "name@hi": "अमित",
            "name@bn": "অমিত",
            "name@en": "Amit",
            "age": 35,
            "friend": [{
                    "uid": "_:michael"
                },
                {
                    "uid": "_:sang"
                },
                {
                    "uid": "_:artyom"
                }
            ]
        },
        {
            "uid": "_:luke",
            "name@pl": "Łukasz",
            "name@en": "Luke",
            "age": 77
        },
        {
            "uid": "_:artyom",
            "name@ru": "Артём",
            "name@en": "Artyom",
            "age": 35
        },
        {
            "uid": "_:sarah",
            "name": "Sarah",
            "age": 55
        },
        {
            "uid": "_:sang",
            "name@ko": "상현",
            "name@en": "Sang Hyun",
            "age": 24,
            "owns_pet": {
                "uid": "_:goldie",
                "name": "Goldie"
            },
            "friend": [{
                    "uid": "_:amit"
                },
                {
                    "uid": "_:catalina"
                },
                {
                    "uid": "_:hyung"
                }
            ]
        },
        {
            "uid": "_:hyung",
            "name@en": "Hyung Sin",
            "name@ko": "형신",
            "friend": {
                "uid": "_:sang"
            }
        },
        {
            "uid": "_:catalina",
            "name": "Catalina",
            "age": 19,
            "owns_pet": {
                "uid": "_:perro",
                "name": "Perro"
            },
            "friend": {
                "uid": "_:sang"
            }
        }
    ]
}

Basic query

query

{
    # #Comments at the beginning
    # everyone1 everyone2 represents multiple query statement IDS
    # Anythofterms represents the query type. This example is Michael Amit for the name attribute.
  everyone1(func: anyofterms(name, "Michael Amit")) {
    # Return volume specifies return data
    name
    friend {
        # Returns the name attribute of friend @ru@ko@en if Ru exists, returns Ru if Ko exists, and returns Ko without returning en 
      name@ru:ko:en
      # Return all attributes without filtering friends
      friend { expand(_all_) { expand(_all_) } }
    }
  }
  michaels_friends(func: eq(name, "Michael")) {
    name
    age
    friend {
      name@.
    }
  }
}

The basic structure is as follows:

{
    # Query id (func query method){
    #    Return field
    #    Return relation {return condition}
    #}
    michaels_friends(func: eq(name, "Michael")) {
    name
    age
    friend {
      name@.
    }
    # Name returns the name, age, friend. name attribute for Michael
  }
}

Column attribute query

data type

Dgraph Type Description
int signed 64 bit integer
float double precision floating point number
string string
bool boolean
id ID's stored as strings
dateTime RFC3339 time format with optional timezone eg: 2006-01-02T15:04:05.999999999+10:00 or 2006-01-02T15:04:05.999999999
geo geometries stored using go-geom
# Query properties for name, age, friend, owns_pet
schema(pred: [name, age, friend, owns_pet]) {
  type # data type
  index # true is that the index can be searched
}

Column returns and aliases

Columns can be @ modified

  1. If there is nothing, the return value, if there is no value, is not returned.
  2. If @Label 1: Label 2: Label 3. Total return to the leftmost column of values.
  3. @ Label 1: Label 2:. If Label 1, Label 2 has no value, return other labels, and if other labels do not return "some"

alias

Alias: Attributes

Aliases can be the same as attribute names

{
  language_support(func: allofterms(name, "Michael")) {
    name@bn:hi:en
    name1 : name@bn:hi:en
    name2 : name
    name3 : name@bn:hi:en:.
    age : age
    friend {
      name@ko:ru
      age
    }
  }
}

Support nesting

Query Michael's friend's pet:

{
  michael_friends_pets(func: eq(name, "Michael")) {
    name
    age
    friend {
      name@.
      owns_pet {
        name
      }
    }
  }
}

Query Michael's friends:

Note that return is not a tree structure to view return specifically

{
  michael_friends_friends(func: allofterms(name, "Michael")) {
    name
    age
    friend {
      name
      friend {
        name
      }
    }
  }
}

Search method

func Meaning
allOfTerms(edge_name, "term1 ... termN") Match a node with an outgoing string edge, edge_name where the string contains all the listed terms.
anyOfTerms(edge_name, "term1 ... termN") Same as allOfTerms, but matching at least one term.
has(edge_name) Does it exist?
eq(edge_name, value) Be equal to
ge(edge_name, value) Greater than or equal to
le(edge_name, value) Less than or equal to
gt(edge_name, value) greater than
lt(edge_name, value) less than
AND and
OR or
NOT wrong
orderasc Birth order
orderdesc Descending order
first: N Returns N result
offset: N Skip the N result
after: uid After returning to uid
count(edge_name) count
{
  michaels_friends_filter(func: allofterms(name, "Michael")) {
    name
    age
    friend @filter(ge(age, 27)) {
      name@.
      age
    }
  }
  michael_friends_and(func: allofterms(name, "Michael")) {
    name
    age
    friend @filter(ge(age, 27) AND le(age, 48)) {
      name@.
      age
    }
  }
  michael_friends_sorted(func: allofterms(name, "Michael")) {
    name
    age
    friend @filter(ge(age, 1) AND le(age, 300))(orderasc: age) {
      name@.
      age
    }
  }
  michael_friends_first(func: allofterms(name, "Michael")) {
    name
    age
    friend (orderasc: name@., offset: 1, first: 2) {
      name@.
    }
  }
  michael_number_friends(func: allofterms(name, "Michael")) {
    name
    age
    count(friend)
  }
  # polymerization
  lots_of_friends(func: ge(count(friend), 2)) {
    name@.
    age
    friend {
      name@.
    }
  }
  # Combining with nodal conditions
  lots_of_friends(func: ge(count(friend), 2)) @filter(ge(age, 20) AND lt(age, 30)) {
    name@.
    age
    friend {
        name@.
    }
  }
  # existence
  have_friends(func: has(friend)) {
    name@.
    age
    number_of_friends : count(friend)
  }

}

Posted by fhil85 on Tue, 13 Aug 2019 20:22:16 -0700