Conditional filter with python client v4

Hi @jhc !!

No worries. I think I got you covered.

Is this what you are looking for?
Given this dataset:

import weaviate
client = weaviate.connect_to_local()
from weaviate.classes.query import Filter

client.collections.delete("Test")
collection = client.collections.create(name="Test")
collection.data.insert({"color": "grau", "price": 300, "location": "Brazil"})
collection.data.insert({"color": "grau", "price": 400, "location": "Netherlands"})
collection.data.insert({"color": "grau", "price": 100, "location": "Netherlands"})
collection.data.insert({"color": "bope", "price": 400, "location": "Egypt"})
collection.data.insert({"color": "bope", "price": 200, "location": "Egypt"})

you can have optional filters added to a base filter, for example:

filter_by_location = True
location = "Brazil"

# now we filter
#base filter
filters=(
    Filter.by_property("color").equal("grau") & 
    Filter.by_property("price").less_or_equal(300)
)

if filter_by_location:
    filters = filters & Filter.by_property("location").equal(location)

query = collection.query.fetch_objects(
    filters=filters
)

for object in query.objects:
    print(object.properties)

Note that you can also do more complex logic, using nested filters.

Like in this example:

response = jeopardy.query.fetch_objects(
    filters=Filter.by_property("answer").like("*bird*") &
            (Filter.by_property("points").greater_than(700) | Filter.by_property("points").less_than(300)),
    limit=3
)

Let me know if this helps!

Thanks!