Copy ObjectID (or perhaps other fields) from all filtered items

Hey :wave:

I have a use case where I’d like to provide a “Copy all” button to my users where they can copy all the items (users in this case) they have filtered in my form.

I.e.
10000 users
Filter by gender - 5000 users
Filter by age - 2500 users
Filter by interest - 200 users

In my case, I would like for the user to be able to copy a comma separated list of the 200 remaining users’ objectId that can be used as a call to my primary API, a la

addUsersToActivity(users: string[])

and pass along the copied values,

addUsersToActivity(users: ['id1', 'id2'...])

Ideally, this would also be possible for other fields, i.e. copy all the chosen user’s firstName value.

How would I go about that with Algolia InstantSearch (React)? :slight_smile:

Hi there,
I’m afraid I don’t understand your question correctly.
If you have a list of objectID, then you can get objects from them.

In the code sandbox above, I added two examples in App.js.
One is getting objects from objectIDs and another one is getting search results based on filters.

Please let me know if it’s not what you asked for. And it always help us a lot if you come up with a small example.
You can use this template: https://codesandbox.io/s/github/algolia/create-instantsearch-app/tree/templates/react-instantsearch

Have a nice day!

Hey there,

Sorry, wasn’t clear enough :slight_smile:

Going from your example, it would work something like this:

  1. Perform some search, in this case I filtered three brands and searched for '55" '. This results in a subset of results, in this example we have 4+1 = 5
  2. Press new button, “Copy all IDs”, which ideally would copy the objectID value for all filtered results (5 in this case) to the user’s clipboard in the format id1,id2,id3,id4,id5 etc

How would I do that? Keeping in mind that it should work even if there are thousands of results, rather than 5

Thanks :+1:

Hi @jm1!

If you want to intercept the results to store them in another variable, one way to do it would be like show in the following code sandbox:

You can plug on the searchClient parameter of the <InstantSearch> widget and from here, retrieve the IDs anytime you’re making a request. Then you can store those ids in a different variable, that you would use for your button. You can get more information on the <InstantSearch> widget and the searchClient parameter in this part of our documentation.

Have a good day,

1 Like

Hey @chloe.liban,

Thanks for chiming in. Not sure I follow completely.

In the codebox you linked, you re-declare the searchClient passed to InstantSearch with some predefined filter variables (.search({ filters: 'brand:Apple' },) - are you suggesting that every time the user makes a search form update, I pass the new search state into the searchClient? If so, won’t that make the whole InstantSearch widget re-render from the top-down every-time the user makes a filter change?

Also, I assume you suggest that I use the res object on line 30 to iterate over the hits on all the returned pages and accumulate the desired fields when the user presses the button, yes?

Hi @jm1,

Thank you for your feedback. I think there was a problem with the sandbox I sent you, could you check it again? The new code should appear now.

2 Likes

Thanks, that makes more sense :smile:

Glad it helped! Don’t hesitate and reach back to us if you have any more questions.

Have a good day,

This is a great question @jm1 and super timely as I’m about to start working on a similar feature on my site. Thank you for posting!

Quick question for @chloe.liban and the team as it might be relevant to this thread: Does the getObjects function have an effective 1,000 record limit?

As per the docs:
By default, Algolia limits the maximum number of hits that can be retrieved via a query to 1000.

That’s correct, that’s why in other examples for this question we have suggested to use browse instead. Hope this helps you: https://codesandbox.io/s/extending-widgets-g8mwu

2 Likes