# Filter out node based on connected node values

**URL:** <https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810>\
**Category:** Dgraph\
**Created:** [May 22, 2020, 12:42pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810 "2020-05-22T12:42:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 22, 2020, 12:42pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/1 "2020-05-22T12:42:49Z")

</div>

Hey,

I have two nodes, lets call them `Person` & `Company`.

The `Person` has an edge named `Worker` pointing to `Company`.  
The company has a `bool` predicate `isDeleted`.

I wish to retrieve a Persons by it `uid` and return nothing if the company `isDeleted` predicate is set to `true`.  
Preferable by using the root func to get the `Person` and not the `Company` and then recourse all decedents.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [May 22, 2020, 1:26pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/2 "2020-05-22T13:26:55Z")

</div>

This worked for me, but is there a better way?

```auto
query {
  var(func: eq(user.id, "8cbe5522-6adf-4f2b-8801-8497736c9342")) {
    user.tenant @filter(eq(tnnt.isDeleted, f)) {
      u1 as uid
    }
  }
  get(func: eq(user.id, "8cbe5522-6adf-4f2b-8801-8497736c9342")) @filter(eq(user.isDeleted, f) and eq(len(u1), 1)) {
    user.id
    user.isEnabled
  }
}

```

---

<div class="post-metadata">

**Author:** ![Naman](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/naman/32/3498_2.png) [@Naman](https://discuss.dgraph.io/u/Naman)\
**Post date:** [June 14, 2020, 2:32pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/3 "2020-06-14T14:32:50Z")

</div>

Hi @mbn18  
With the [`@cascade`](https://dgraph.io/docs/query-language/#cascade-directive) directive, nodes that don’t have all predicates specified in the query are removed. Let’s work out for your use case.

**Data:**

```auto
{
  set {
    _:company1 <name> "CompanyABC" .
    _:company1 <dgraph.type> "Company" .
    _:company1 <isDeleted> "true" .
    
   _:naman <worker> _:company1 .
    _:naman <dgraph.type> "Person" .
    _:naman <name> "naman" .
  }
}

```

**Query:**

```auto
{
 # UID of naman
  foo(func: uid(0xfffd8d67d83ccb99)) @cascade{
    name
    worker @filter(eq(isDeleted, false)){
      name
      uid
    }
  }
}

```

If there be another company whose isDeleted is false and its name predicate is set, then you would get a single matching company. If any of the predicate is missing from the query traversal, all predicates are removed.

---

<div class="post-metadata">

**Author:** ![Neeraj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/neeraj/32/3104_2.png) [@Neeraj](https://discuss.dgraph.io/u/Neeraj)\
**Post date:** [June 15, 2020, 5:14am UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/4 "2020-06-15T05:14:10Z")

</div>

Both of the solutions will work fine. But these will be slow on a large amount of data if `isDeleted` predicate is indexed [(reference)](https://github.com/dgraph-io/dgraph/issues/2713) and it’ll be better to keep `isDeleted` predicate unindexed.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [June 19, 2020, 2:07pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/5 "2020-06-19T14:07:39Z")

</div>

> [@Neeraj](#):
>
> it’ll be better to keep `isDeleted` predicate unindexed.

@Neeraj, interesting, so I should remove the index from `isDeleted` predicate if that predicate is used only in `@filter`?

---

<div class="post-metadata">

**Author:** ![Neeraj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/neeraj/32/3104_2.png) [@Neeraj](https://discuss.dgraph.io/u/Neeraj)\
**Post date:** [June 20, 2020, 6:02am UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/6 "2020-06-20T06:02:42Z")

</div>

Well, it has more to do with the number of different values a predicate can have rather than if it is used in the filter. For eg. in your case `isDeleted` can only have two different values, `True` or `False`.

Lets say you have 10 million nodes in your dataset. Then the posting list of `(<isDeleted>, true)` will have almost 5 million nodes and same for the `(<isDeleted>, false)` case. Hence iterating over them will be a very expensive operation.

If you remove index from `isDeleted` then it’ll be stored as `<predicate, uid> => [value1, value2 ....]` and in your case as `<isDeleted, 0x123> => [True]` and accessing this value will be very efficient.

In the reference I provided above, query with index for `sex` predicate takes around `300 ms` whether `sex` predicate is used in the `@filter` or in the main function and it takes around `3 ms` if we don’t index the `sex` predicate (this time `sex` predicate can be only used in the `@filter` as the predicate of main query is required to be indexed).

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [June 21, 2020, 1:49pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/7 "2020-06-21T13:49:12Z")

</div>

Hi @Neeraj,

I removed the `bool` index and to query actualy got slower.

Here is a simple example:

```auto
{
  q(func:type(Measurement)) @filter(eq(meas.isDeleted, f)) {
    count(uid)
  }
}

```

Without bool index:

```auto
{
  "data": {
    "q": [
      {
        "count": 99307
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 163375,
      "processing_ns": 203839418,
      "encoding_ns": 9079376,
      "total_ns": 213195197
    },
    "txn": {
      "start_ts": 29146
    },
    "metrics": {
      "num_uids": {
        "_total": 99307,
        "dgraph.type": 0,
        "meas.isDeleted": 99307
      }
    }
  }
}

```

With bool index:

```auto
{
  "data": {
    "q": [
      {
        "count": 99307
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 163287,
      "processing_ns": 8838663,
      "encoding_ns": 8884165,
      "total_ns": 17969863
    },
    "txn": {
      "start_ts": 29163
    },
    "metrics": {
      "num_uids": {
        "_total": 99307,
        "dgraph.type": 0,
        "meas.isDeleted": 99307
      }
    }
  }
}

```

So, by adding the index the query took 1/10 of the previous

Thx

---

<div class="post-metadata">

**Author:** ![Neeraj](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/neeraj/32/3104_2.png) [@Neeraj](https://discuss.dgraph.io/u/Neeraj)\
**Post date:** [June 23, 2020, 8:53am UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/8 "2020-06-23T08:53:19Z")

</div>

Hey @mbn18,

Can you please share a drive link for your data? I’ll look into it.

---

<div class="post-metadata">

**Author:** ![mbn18](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mbn18/32/1139_2.png) [@mbn18](https://discuss.dgraph.io/u/mbn18)\
**Post date:** [June 25, 2020, 1:49pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/9 "2020-06-25T13:49:34Z")

</div>

@Neeraj, Will upload the data (only part of it) to Dgraph customer support channel and open a ticket about this

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [July 25, 2020, 1:49pm UTC](https://discuss.dgraph.io/t/filter-out-node-based-on-connected-node-values/6810/10 "2020-07-25T13:49:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
