FQLX: How do I Query for entries with their ts within a range?

I have a collection with a lot of docs in it. So I created an Index for my collection so I can deal with only the docs for a specific client within my system, based on the collections clientId field. Although, I want to filter down even further to only the docs for this client with a ts value within a time range.

I’ve found a way to do this, by creating an index with a term of “clientId”, and a value of “ts”. From the documentation it looks like the values in an index are used for ordering, or range kind of uses. I tried to put “ts” as a term in the index, but Fauna rejected it.

With this index, I queried it only looking for documents from today. (Though I do need to add in the ability to query for documents with ts between two Time values.)

CollectionName.byMyIndexName(Client.byId(“some_client_id”)).where(.ts > Time(“2023-07-24T00:00:00Z”)).count()

This works, but is slow. It takes a few seconds. I’m concerned that my index isn’t putting the records in order of their ts, so Fauna isn’t able to get to just the records for the time range quickly.

What am I doing wrong? How should I be querying in FQLv10 (FQLX) for documents for a specific .clientId and with a ts value within a specific Time range?

Thank you in advance for your help!

I received the answer in the Fauna Discord Help channel…

The ts value is the right idea, you just need to set it in the range argument of an index call:

CollectionName.byMyIndexName(client_id, { from: Time.now().subtract(1, "day") })

Indexes take an optional last argument, which is an object with the optional fields from and to . range is documented in here: https://fqlx-beta--fauna-docs.netlify.app/fqlx/beta/reference/schema_entities/collection/indexes

1 Like

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