#community-help

Retrieving Multiple Documents by ID in Typesense

TLDR Bruno asked about retrieving multiple documents by ID in Typesense. Jason suggested using filter_by: id:= and gave advice on POST requests and order preservation. Bruno successfully implemented these solutions.

Powered by Struct AI

3

1

1

Jun 15, 2023 (6 months ago)
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
10:38 PM
Quick question, not sure if I missed it: is there a way to retrieve multiple documents by ID in one request? Use case is that we run a ~90k document lunr index in the user's browser, so that search feels instant even when connection is crap, but that only holds title and ID for obvious resource contraint reasons, and we'd love to in a second step then augment those results by fetching the remaining data out of Typesense.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:39 PM
Yup, you can do filter_by: id:=[1,2,3]
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
10:41 PM
Perfect!
10:45
Bruno
10:45 PM
It's a special extra layer for one of 4 indices, the other ones already work like a charm directly out of Typesense, where federated hums with lots of data and vector searches are doing exactly what they should, really solid job!
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:46 PM
That’s great to hear! 🙏
Jul 03, 2023 (5 months ago)
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
07:51 PM
Uhm, stupid question Jason re fetching a set of docs as per above: is there a way to POST a simple search (JSON \q:'*', filter...) in a collection or do I always need to build a GET request string?
07:52
Bruno
07:52 PM
Tried posting to <http://localhost:8108/collections/foo/documents/search>
07:52
Bruno
07:52 PM
Which 404s
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:53 PM
You can send a POST with a JSON structure to the multi_search endpoint even it’s just a single search
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
07:54 PM
Got it, thats the one we're using so far - performance should be the same?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:54 PM
Yup
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
07:57 PM
Thanks a lot as always!

1

08:15
Bruno
08:15 PM
Quick follow up: works, but the results have a different order than the array of IDs we filtered by - is there a way to preserve it?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:15 PM
Filtering is not order-preserving… You would have to use the sort_by parameter to preserve order, or alternatively use the pinned_hits search parameter
Bruno
Photo of md5-d9ca032e3941589aafa8433269974f96
Bruno
08:20 PM
Got it, its a short list with no predictable order we can pass to typesense sort_by so think sorting it in the client makes most sense.

1

08:30
Bruno
08:30 PM
Darn, its such a pleasure working with Typesense, it's when youre not sure and the give it a shot anyway and it just works flawlessly...

1

08:30
Bruno
08:30 PM
DId this.current.results.plants.items.map(function(el,i){order += el.id + ':' + (i+1) + ',';buffer.push(parseInt(el.id))})
08:30
Bruno
08:30 PM
and used pinned and works like a charm, thank you!

1

1

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community