How to filter by date field

Hi, how do I filter searches by date using the python client?

I can see in the docs how to filter by internal datetime objects: link

Which mentions you must set indexTimestamps to true to track these meta timestamps: Link

But I don’t see anything on how to filter by a user defined timestamp.

I have a field published_at defined as such:

...
wc.Property(
        name="published_at",
        data_type=wc.DataType.DATE,
        index_searchable=False,
        skip_vectorization=True,
    ),
...

Which is returned in the graphql client as a datetime: "published_at": "2024-04-22T08:16:47Z",

In my index I may have some published_at times that are in the future that I don’t want to retrieve. I would like to filter less than or equal to today’s date (or less than tomorrow’s date).

How is this done using the python client?

Thanks

Solved it using regular property filter and a datetime. For anyone that may find it useful:

def get_tomorrow_midnight() -> datetime:
    now = datetime.now()
    tomorrow = now + timedelta(days=1)
    midnight = datetime(tomorrow.year, tomorrow.month, tomorrow.day)
    return midnight
response = collection.query.hybrid(
            ...,
            filters=wq.Filter.by_property("published_at").less_than(
                get_tomorrow_midnight()
            ),
        )

May be worth adding something to the docs, as currently the only datetime filtering in the docs is displaying how to filter on weaviate’s meta timestamps, not user-defined ones (unless I missed it).

Cheers

1 Like

hi @justin.godden !

Thanks for sharing and pointing it out.

At the end of the day, the indexTimestamps fields are just a date field as published_at are :slight_smile:

So all date filters will also apply to both meta properties and the ones you define.

Thanks!

Hi Duda,

The only point I was making was the only example of datetime filtering in the docs is using a built-in method specifically for the meta properties - by_creation_time - from the first link: filters=wvc.query.Filter.by_creation_time().greater_or_equal(year2k)

Unless I missed it, I didn’t see anywhere explaining filtering user defined datetimes. Perhaps it’s implied since you can do value comparison with datetimes already in python (just like using less than for strings works also).

Just my experience that I didn’t find it clear. Up to you if you want to act on that.

Cheers

1 Like

we will add some examples :slight_smile:

Oh! Got it.

It indeed makes sense to have an explicit example there as well as the reference for the meta properties.

Thanks!!