No need for @cascade
, so as it seems you don’t need a smaller time indexing, so that’s the whole query below.
Schema
<humidity_measurement>: uid @reverse .
<location>: string @index(exact) .
<temperature_measurement>: uid @reverse .
<ts>: datetime @index(hour) .
<type>: string @index(exact) .
<value>: int @index(int) .
Mutation
{ "set": [
{
"uid": "0x1",
"type": "Sensor",
"location": "New York",
"temperature_measurement": [
{
"uid": "0x2",
"type": "Temperature",
"ts": "2019-07-15T16:18:09.877394",
"value": 32
},
{
"uid": "0x3",
"type": "Temperature",
"ts": "2019-06-20T16:20:12.287947",
"value": 51
}
],
"humidity_measurement": [
{
"uid": "0x4",
"type": "Humidity",
"ts": "2019-07-21T16:27:28.516389",
"value": 60
},
{
"uid": "0x5",
"type": "Humidity",
"ts": "2019-06-20T16:27:41.429091",
"value": 70
}
]
},
{
"uid": "0x6",
"type": "Sensor",
"location": "Anchorage",
"temperature_measurement": [
{
"uid": "0x7",
"type": "Temperature",
"ts": "2019-06-20T16:29:05.953131",
"value": 10
},
{
"uid": "0x8",
"type": "Temperature",
"ts": "2019-06-20T16:29:20.366124",
"value": 33
}
],
"humidity_measurement": [
{
"uid": "0x9",
"type": "Humidity",
"ts": "2019-06-20T16:28:34.131180",
"value": 20
},
{
"uid": "0xa",
"type": "Humidity",
"ts": "2019-06-20T16:28:52.505992",
"value": 30
}
]
}
]}
Query
{
G as var(func: eq(type, Sensor))
#This Block is
#"I want to identify the sensors where there exists a temperature reading above 31 (which both satisfy) "
{
temperature_measurement @filter(gt(value, 31)){
d as uid
}
}
#This Block is
#I need to catch the time from the "humidity measurement that occurs after the first temperature reading above 31" so I need the "2019-07-15T16:18:09.877394" temp to use in the next block.
var(func: uid(d), first:1, orderdesc: ts) {
dF as ts
}
#In this block we gonna filter the humidity by the "first temperature above 31"
var(func: uid(G)){
humidity_measurement @filter(gt( ts, val(dF) ))
{
~humidity_measurement {
final as uid
# location
}
}
}
#Now is the final query block. In above blocks we filtered as you needed so now we apply the results.
myQuery(func: uid(final)){
uid
type
location
temperature_measurement {
uid
type
ts
value
}
humidity_measurement
# @filter(gt( ts, val(dF) )) #use this filter if needed
{
uid
type
ts
value
}
}
}
Result
{
"extensions": {
"server_latency": {
"parsing_ns": 20800,
"processing_ns": 9674100,
"encoding_ns": 706600
},
"txn": {
"start_ts": 247
}
},
"data": {
"myQuery": [
{
"uid": "0x1",
"type": "Sensor",
"location": "New York",
"temperature_measurement": [
{
"uid": "0x2",
"type": "Temperature",
"ts": "2019-07-15T16:18:09.877394Z",
"value": 32
},
{
"uid": "0x3",
"type": "Temperature",
"ts": "2019-06-20T16:20:12.287947Z",
"value": 51
}
],
"humidity_measurement": [
{
"uid": "0x4",
"type": "Humidity",
"ts": "2019-07-21T16:27:28.516389Z",
"value": 60
},
{
"uid": "0x5",
"type": "Humidity",
"ts": "2019-06-20T16:27:41.429091Z",
"value": 70
}
]
}
]
}
}