#community-help

Efficient Method to Find Records at Interval Timestamps

TLDR Dan asked for an efficient method for counting frequency of record intervals. Jason suggested using multi_search or creating a new field for each record.

Powered by Struct AI

2

1

1

12
16mo
Solved
Join the chat
Jul 06, 2022 (16 months ago)
Dan
Photo of md5-f8f7b04dda4a09dec991cdc07f4d9c64
Dan
03:16 PM
I have a numeric timestamp field in our record - what’s the most efficient way to find out how many records are at intervals of this value? multi_search..?

1

Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:09 PM
Dan You could do something like:

filter_by: timestamp:>=123 && timestamp:<=345
Dan
Photo of md5-f8f7b04dda4a09dec991cdc07f4d9c64
Dan
04:11 PM
this works great - but I’m wondering if there’s any way I can get counts per interval in a single query, as if it were a facet, for example
04:12
Dan
04:12 PM
ie. with a search for all documents, I want to know how many are in buckets of intervals x > y and y > z etc etc
04:13
Dan
04:13 PM
seems like the only way to get that answer is to do a multi_query, and plumb in the data somehow
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:14 PM
Yeah multi_search is one way, the other way would be to add a new field to every record called say timestamp_interval and set a common value for all records between x > y, and another common value for all records between y > z, and then facet by that field.
04:15
Jason
04:15 PM
For eg, in the songs-search demo: https://songs-search.typesense.org/?songs_1630520530850%5Bquery%5D=Annie%27s%20song%20Denver

The actual release date is a unix timestamp, but I created a new field called "Release Date" and extracted just the year into a new string field and I facet by that field for display / filtering purposes, but use the timestamp field for sorting

1

Dan
Photo of md5-f8f7b04dda4a09dec991cdc07f4d9c64
Dan
04:17 PM
works great for absolute dates, but I’m looking for relative dates, sadly
04:17
Dan
04:17 PM
ie, last 7 days, last 30..
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:18 PM
Ah, then multi_search would be the only way
Dan
Photo of md5-f8f7b04dda4a09dec991cdc07f4d9c64
Dan
04:18 PM
all good. I’ll give it a go 🙂

1

05:28
Dan
05:28 PM
working great.

1