[Need Help] Browse with pagination
Closed this issue · 3 comments
So I have this use case where I need to browse more than 1000 items per page, so I use browse
method, but since it have pagination I also use helper.setPage(xx)
, where helper is algoliasearchhelper & xx is the number of page, it works fine for page 0, 1, 2 but it started to broke when page larger than 3, it returns you should pass cursor of current page to fetch next page
. What should I do to fix this?
Code snippet:
export function browseSearchWithPaginationResults(
searchParameters,
searchIndex,
APIKey = ALGOLIA_API_KEY,
) {
const algolia = algoliasearch(ALGOLIA_APP_NAME, APIKey);
const index = algolia.initIndex(searchIndex);
const helper = algoliasearchHelper(algolia, searchIndex);
Object.entries(searchParameters).map(([k, v]) => {
helper.setQueryParameter(k, v);
});
const { page } = searchParameters;
if (page) {
helper.setPage(page);
}
const queries = requestBuilder._getQueries(searchIndex, helper.state);
const queryParams = queries[0].params;
return new Promise((resolve, reject) => {
index.browse(queryParams, (err, _hits) => {
const hits = _hits || { hits: [] };
resolve({
searchIndex: searchIndex,
searchParameters: Object.assign({}, helper.getState()),
content: searchResults,
});
});
});
}
Hello @kurokochin, looks like you made an empty issue. Please elaborate in the responses what your question is.
Thanks 👋!
I already add the description.
Hmm, I've been trying this out, and it works fine for me, only change I made is making it work in node and use an API Key which had the browse ACL (do you have that too)?
const algoliasearchHelper = require('algoliasearch-helper');
const requestBuilder = require('algoliasearch-helper/src/requestBuilder');
const algoliasearch = require('algoliasearch');
const { SearchResults } = algoliasearchHelper;
const ALGOLIA_API_KEY = ''; //(send me an email and I can give you an API Key on my index to try this out)
const ALGOLIA_APP_NAME = 'OFCNCOG2CU';
function browseSearchWithPaginationResults(
searchParameters,
searchIndex,
APIKey = ALGOLIA_API_KEY
) {
const algolia = algoliasearch(ALGOLIA_APP_NAME, APIKey);
const index = algolia.initIndex(searchIndex);
const helper = algoliasearchHelper(algolia, searchIndex);
helper.state.setQueryParameters(searchParameters);
const { page } = searchParameters;
if (page) {
helper.setPage(page);
}
const queries = requestBuilder._getQueries(searchIndex, helper.state);
const queryParams = queries[0].params;
return new Promise((resolve, reject) => {
index.browse(queryParams, (err, _hits) => {
if (err) {
reject(err);
}
const hits = _hits || { hits: [] };
let searchResults;
try {
searchResults = new SearchResults(helper.getState(), [
Object.assign({}, hits, { query: '' }),
hits,
]);
} catch (e) {
const newHelper = algoliasearchHelper(algolia, searchIndex);
searchResults = new SearchResults(newHelper.getState(), [
Object.assign({}, hits, { query: '' }),
hits,
]);
newHelper.state.setQueryParameters(searchParameters);
// you probably want to do something with `newHelper`?
} finally {
resolve({
searchIndex,
searchParameters: Object.assign({}, helper.getState()),
content: searchResults,
});
}
});
});
}
browseSearchWithPaginationResults(
{
query: 'hi there',
page: 819,
},
'npm-search'
)
.then(({ content: { hits, nbHits } }) =>
console.log({ nbHits, hits: hits.length })
)
.catch(console.log);