Nested / cyclic filtering not working

ppp225 commented :

Thanks for your response @MichelDiz !
I already tried the query with multiple blocks, and it didn’t work for my use case. It works for a single element (Speed in this case), however not for multiple.
I updated the mutation above, so please reimport it, and try this query:

 {
 DEALERS as var(func: Type(Dealer)) {
    dealerName
  	~contractDealer {
      contractID
      TOO_MANY_CARS as ~carContract
    }
    val(TOO_MANY_CARS)
  }
 dealer(func: uid(DEALERS)) {
    dealerName
  	~contractDealer {
      contractID
      REQUIRED_CAR as ~carContract {
        carName     # should only include this car
      }
      val(REQUIRED_CAR)
      contractGroup {
        groupName
        groupStat {
          statName
          ~carStat @filter(uid(TOO_MANY_CARS)){ # doesn't work, includes both cars
          # ~carStat @filter(uid(REQUIRED_CAR)){ # doesn't work, result is empty
          # ~carStat { # doesn't work, result has all cars
            carName
          }
        }
      }
    }
  }
}

The result, which I get, looks like this

  "data": {
    "dealer": [
      {
        "dealerName": "Dealer2",
        "~contractDealer": [
          {
            "contractID": "222",
            "~carContract": [
              {
                "carName": "Porsche"   # should only include this car
              }
            ],
            "contractGroup": {
              "groupName": "SpeedGroup",
              "groupStat": {
                "statName": "Speed",
                "~carStat": [
                  {
                    "carName": "Ferrari"   # this should be filtered out
                  },
                  {
                    "carName": "Porsche"   # should only include this car
                  }
                ]
              }
            }
          },
(...)

Above is the query related to this Issue.

Full use case

Below, I added my full use case, where I require this functionality - in case you find it useful. What is done here, is calculating weighted scores based on facet data and some weights stored in nodes. I want to use this query in an upsert (to save the final calculated score in a Node).

 {
 DEALERS as var(func: Type(Dealer)) {
    dealerName
  	~contractDealer {
      contractID
      TOO_MANY_CARS as ~carContract
    }
    val(TOO_MANY_CARS)
  }
 dealer(func: uid(DEALERS)) {
    dealerName
  	~contractDealer {
      contractID
      REQUIRED_CAR as ~carContract {
        carName     # should only include this car
      }
      val(REQUIRED_CAR)
      contractGroup {
        groupName
        WEIGHT as math(0.12) # would be saved in DB as groupWeight
        groupStat {
          statName
          ~carStat @facets(STAT_VALUE as statValue) @filter(uid(TOO_MANY_CARS)){ # doesn't work, includes both cars
          #~carStat @facets(STAT_VALUE as statValue) @filter(uid(REQUIRED_CAR)){ # doesn't work, result is empty
          # ~carStat @facets(STAT_VALUE as statValue) { # doesn't work, result has all cars
            carName
            sv1: val(STAT_VALUE) # should have only value from one car
          }
          sv2: SV2 as sum(val(STAT_VALUE)) # equal to sv1
        }
        sv3: SV3 as sum(val(SV2)) # equal to sv2 and sv1
        weightedScore: WS as math(WEIGHT * SV3)
      }
      weight2: WEIGHT2 as sum(val(WEIGHT)) # propagate down
      weightedScore2: WS2 as sum(val(WS)) # propagate down
    }
    weightTotal: WEIGHT3 as sum(val(WEIGHT2)) # aggregate
    scoreTotal: WS3 as sum(val(WS2))  # aggregate
    score: math(WS3 / WEIGHT3)  # finally calculate weighted score
  }
}

Of course for this query to work, I need:

  • improved filtering, as described in this issue,
  • correct facet values, which I described in another issue.
1 Like