Get all object ids in index

Hello everyone.
I must generate a document with a list of all object IDs in Algolia.

I tried to write a javascript script to paginate and get the list of IDs, but the limit of search if 5000 and I have more than 60k documents.

(async () => {
  let ids = [];
  let page = 0;

  while (true) {
    /** @type {typeof import("algoliasearch").SearchResponse<any> } */
    const result = await index
      .search('', { hitsPerPage: 200, attributesToRetrieve: ['id'], page })
      .catch((e) => console.log(e.message));


    if (result.hits.length === 0) {
      break;
    }
    ids = [...ids, ...result.hits.map((h) => h.id)];

    page += 1;
  }

  console.log(ids.length);
})();

But the result is: 5000

I want to get all IDs in my base. How can I do to get without this pagination limitation?

Thanks for this

Welcome to the community!

It may be easier to instead download/browse the index and work with it locally rather than try to paginate all of the results from the API.

Here’s a JavaScript example:

const algoliasearch = require('algoliasearch');

// Use an API key with `browse` ACL
const client = algoliasearch('app-id', 'admin-api-key');
const index = client.initIndex('index-name');

let hits = [];

// Get all records as an iterator
index.browseObjects({
  batch: batch => {
    hits = hits.concat(batch);
  }
})
  .catch(err => console.error(err))
  .then(() => console.log(hits.length));
1 Like