# Filtering by IsNull on a cross-referenced object is not working

**URL:** <https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132>\
**Category:** Support\
**Created:** [June 6, 2023, 4:53am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132 "2023-06-06T04:53:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamsi](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@vamsi](https://forum.weaviate.io/u/vamsi)\
**Post date:** [June 6, 2023, 4:53am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/1 "2023-06-06T04:53:15Z")

</div>

I have two classes `Article` and `Metadata` and configured `"indexNullState": true` in `invertedIndexConfig` on both of them.

When I filter from `Article` on `{path: ["hasMetadata","Metadata","article_name"], valueBoolean: true, operator: IsNull}` it yields empty results whereas if I filter it directly on the `Metadata` class it gives me some results.

---

<div class="post-metadata">

**Author:** ![sebawita](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/sebawita/32/5_2.png) [@sebawita](https://forum.weaviate.io/u/sebawita)\
**Post date:** [June 7, 2023, 4:58pm UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/2 "2023-06-07T16:58:15Z")

</div>

hey @vamsi,

This is a great question.

1. Can you share both queries in full?
2. What happens when you run the query with `valueBoolean: false`?

> a hint, you can wrap a whole code example with ``` before and after the code - to display it in a nice format.

---

<div class="post-metadata">

**Author:** ![vamsi](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@vamsi](https://forum.weaviate.io/u/vamsi)\
**Post date:** [June 8, 2023, 12:56pm UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/3 "2023-06-08T12:56:31Z")

</div>

Hey @sebawita ,

Here is an example with dummy data:

```auto
import weaviate
from weaviate.util import generate_uuid5

client = weaviate.Client("http://localhost:8080")

schema = {
  "classes": [{
    "class": "Article",
    "description": "An article",
      "invertedIndexConfig": {
        "indexNullState": True,
    },
    "properties": [
      {
        "dataType": [
          "text"
        ],
        "description": "Article text",
        "name": "article_text"
      },
      {
        "dataType": [
          "Metadata"
        ],
        "description": "Article metadata",
        "name": "hasMetadata"
      }
    ]
  },{
    "class": "Metadata",
    "description": "Article Metadata",
      "invertedIndexConfig": {
        "indexNullState": True,
    },
    "properties": [
      {
        "dataType": [
          "text"
        ],
        "description": "Title of the article",
        "name": "title"
      },
      {
        "dataType": [
          "text"
        ],
        "description": "The author of the article",
        "name": "author_name"
      }
    ]
  }]
}

client.schema.create(schema)

# Metadata - 1
data_object={
    'title': 'first article title'
}

uuid = client.data_object.create(
    data_object=data_object,
    class_name='Metadata',
    uuid=generate_uuid5('first article title',"Metadata"),
    vector=[0.12345] * 1536
)

print(uuid) #19e253f5-c5ad-5031-b37e-1c3fb4d1e42c

# Metadata - 2
data_object={
    'author_name': 'second article author name'
}

uuid = client.data_object.create(
    data_object=data_object,
    class_name='Metadata',
    uuid=generate_uuid5('second article author name',"Metadata"),
    vector=[0.12345] * 1536
)

print(uuid) #ad5646e3-756d-5290-b682-814d3f301bb2

#Article - 1
data_object={
    'article_text': 'first article'
}

uuid = client.data_object.create(
    data_object=data_object,
    class_name='Article',
    uuid=generate_uuid5('first article',"Article"),
    vector=[0.12345] * 1536
)

print(uuid) #1cb50188-c31d-568c-9c4b-c778363bfd5c

# Article -2
data_object={
    'article_text': 'second article'
}

uuid = client.data_object.create(
    data_object=data_object,
    class_name='Article',
    uuid=generate_uuid5('second article',"Article"),
    vector=[0.12345] * 1536
)

print(uuid) #314848d1-930a-5110-bfc5-dc7e4b51405f

# add cross references
# first article -> first metadata
client.data_object.reference.add(
    from_class_name="Article",
    from_uuid="1cb50188-c31d-568c-9c4b-c778363bfd5c",
    from_property_name="hasMetadata",
    to_class_name="Metadata",
    to_uuid="19e253f5-c5ad-5031-b37e-1c3fb4d1e42c",
)

#second article -> second metadata
client.data_object.reference.add(
    from_class_name="Article",
    from_uuid="314848d1-930a-5110-bfc5-dc7e4b51405f",
    from_property_name="hasMetadata",
    to_class_name="Metadata",
    to_uuid="ad5646e3-756d-5290-b682-814d3f301bb2",
)

```

Queries on `Article` class:

Article - fetch Article objects where Metadata `title` is `null`

```auto
query Get {
    Get {
        Article(
            where: {path: ["hasMetadata", "Metadata", "title"], valueBoolean: true, operator: IsNull}
        ) {
            article_text
            hasMetadata {
                ... on Metadata {
                    title
                }
            }
        }
    }
}

```

Response:

```auto
{
    "data": {
        "Get": {
            "Article": [
                {
                    "article_text": "first article",
                    "hasMetadata": [
                        {
                            "title": "first article title"
                        }
                    ]
                },
                {
                    "article_text": "second article",
                    "hasMetadata": [
                        {
                            "title": null
                        }
                    ]
                }
            ]
        }
    }
}

```

Article - fetch Article objects where Metadata `title` is not `null`

```auto
query Get {
    Get {
        Article(
            where: {path: ["hasMetadata", "Metadata", "title"], valueBoolean: false, operator: IsNull}
        ) {
            article_text
            hasMetadata {
                ... on Metadata {
                    title
                }
            }
        }
    }
}

```

Response:

```auto
{
    "data": {
        "Get": {
            "Article": []
        }
    }
}

```

_ **Queries on `Metadata` class:** _

Metadata - fetch Metadata where `title` is `null`

```auto
query Get {
    Get {
        Metadata(where: {operator: IsNull, path: ["title"], valueBoolean: true}) {
            author_name
            title
        }
    }
}

```

Response:

```auto
{
    "data": {
        "Get": {
            "Metadata": [
                {
                    "author_name": "second article author name",
                    "title": null
                }
            ]
        }
    }
}

```

Metadata - fetch Metadata where `title` is not`null`

```auto
query Get {
    Get {
        Metadata(where: {operator: IsNull, path: ["title"], valueBoolean: false}) {
            author_name
            title
        }
    }
}

```

Response:

```auto
{
    "data": {
        "Get": {
            "Metadata": [
                {
                    "author_name": null,
                    "title": "first article title"
                }
            ]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![sebawita](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/sebawita/32/5_2.png) [@sebawita](https://forum.weaviate.io/u/sebawita)\
**Post date:** [June 9, 2023, 1:48pm UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/4 "2023-06-09T13:48:36Z")

</div>

hey @vamsi,  
Thank you for sharing all the examples. 🙏  
I talked with one of our engineers, and he suspects this might be a bug.  
We are investigating the issue.

I will give you a shout once I hear more 👍  
Hopefully, we can find a fix and release an update 😉

---

<div class="post-metadata">

**Author:** ![sebawita](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/sebawita/32/5_2.png) [@sebawita](https://forum.weaviate.io/u/sebawita)\
**Post date:** [June 14, 2023, 7:01am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/5 "2023-06-14T07:01:39Z")

</div>

Hi @vamsi, this was a bug and we found a fix for it. Which will be included in the next minor release.

Here is the [PR](https://github.com/weaviate/weaviate/pull/3131) with the fix.

Thank you for helping us identify the issue. 🙂

---

<div class="post-metadata">

**Author:** ![2020ashish](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/2020ashish/32/797_2.png) [@2020ashish](https://forum.weaviate.io/u/2020ashish)\
**Post date:** [January 8, 2024, 10:50am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/6 "2024-01-08T10:50:26Z")

</div>

Hi @sebawita I have encountered same problem on weaviate 1.23

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [January 8, 2024, 8:20pm UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/7 "2024-01-08T20:20:46Z")

</div>

hi @2020ashish

I have checked, and this is fixed since 1.19, check here a working python notebook:

> **[Google Colaboratory](https://colab.research.google.com/drive/1a6QL_b2IP2aDCXnIEFpdxPnhe8VhR-sy?usp=sharing)**

Note that you need to explicitly create your class with:

```auto
    "invertedIndexConfig": {
        "indexNullState": True
    },

```

as stated here:

> **[Collection schema | Weaviate - Vector Database](https://weaviate.io/developers/weaviate/config-refs/schema#invertedindexconfig--indexnullstate)**
>
> Introduction

Let me know if that works for you 🙂

---

<div class="post-metadata">

**Author:** ![2020ashish](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/2020ashish/32/797_2.png) [@2020ashish](https://forum.weaviate.io/u/2020ashish)\
**Post date:** [January 9, 2024, 8:05am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/8 "2024-01-09T08:05:35Z")

</div>

@DudaNogueira

```auto
Here is my code :
I was checking if this work for "hasCategory"
where_filter = {
    "path": ["hasCategory"],
    "operator": "IsNull",
    # "valueBoolean": True,
    "valueBoolean": False
}
It shows anonymous behaviour

```

Here is query`where\_filter = {  
“path”: [“hasCategory”],  
“operator”: “IsNull”,  
# “valueBoolean”: True,  
“valueBoolean”: False  
}

response = (  
client.query  
.get(“JeopardyQuestion”, [“question”, “answer”, “hasCategory {… on JeopardyCategory{title field}}”])  
.with\_where(where\_filter)  
.with\_limit(10)  
.do()  
)

print(json.dumps(response, indent=2))`

Response : {  
“data”: {  
“Get”: {  
“JeopardyQuestion”:   
}  
}  
}

When i changed "where\_filter = {  
“path”: [“hasCategory”],  
“operator”: “IsNull”,  
“valueBoolean”: True,  
# “valueBoolean”: False  
}

response = (  
client.query  
.get(“JeopardyQuestion”, [“question”, “answer”, “hasCategory {… on JeopardyCategory{title field}}”])  
.with\_where(where\_filter)  
.with\_limit(10)  
.do()  
)

print(json.dumps(response, indent=2))"  
reponse : {  
“data”: {  
“Get”: {  
“JeopardyQuestion”: [  
{  
“answer”: “the answer about one thing”,  
“hasCategory”: [  
{  
“field”: null,  
“title”: “Animals”  
}  
],  
“question”: “Some question about one thing”  
},  
{  
“answer”: “the answer about other thing”,  
“hasCategory”: [  
{  
“field”: “something”,  
“title”: “Places”  
}  
],  
“question”: “Some question about other thing”  
},  
{  
“answer”: “the answer about Name”,  
“hasCategory”: null,  
“question”: “Some question about Name”  
}  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [January 24, 2024, 7:07pm UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/9 "2024-01-24T19:07:41Z")

</div>

Hi!

this is how you can filter by “no cross reference”

```auto
where_filter = {
    "path": ["hasCategory"],
    "operator": "Equal",
    "valueInt": 0
}

```

this is the equivalent for pyv4:

```auto
import weaviate
import weaviate.classes as wcs

client =weaviate.connect_to_local()
client.collections.get("JeopardyQuestion")

items = collection.query.fetch_objects(
    filters=wcs.Filter("hasCategory").equal(0)
)

```

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [January 26, 2024, 4:13am UTC](https://forum.weaviate.io/t/filtering-by-isnull-on-a-cross-referenced-object-is-not-working/132/10 "2024-01-26T04:13:47Z")

</div>

with the latest beta you can do

```auto
Filter.by_ref_count("hasCategory").equal(0)

```
