Created
February 20, 2020 14:53
-
-
Save Haroenv/75d6d762fdb2ff062f18b909d7cda0b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import algoliasearch from 'algoliasearch'; | |
import { encode, addMethods } from '@algolia/client-common'; | |
import { MethodEnum } from '@algolia/requester-common'; | |
import { | |
createBrowsablePromise, | |
browseObjects, | |
BrowseResponse, | |
SearchResponse, | |
ObjectWithObjectID, | |
} from '@algolia/client-search'; | |
const customBrowse: typeof browseObjects = base => { | |
return ({ | |
limit, | |
...requestOptions | |
}: ReturnType<typeof browseObjects> & { limit: number }) => { | |
return createBrowsablePromise({ | |
...requestOptions, | |
shouldStop: ( | |
response: SearchResponse & BrowseResponse<ObjectWithObjectID> | |
) => { | |
return ( | |
response.cursor === undefined || | |
(response.page + 1) * response.hitsPerPage >= limit | |
); | |
}, | |
request: data => | |
base.transporter.read( | |
{ | |
method: MethodEnum.Post, | |
path: encode('1/indexes/%s/browse', base.indexName), | |
data, | |
}, | |
requestOptions | |
), | |
}); | |
}; | |
}; | |
// then add this to your client | |
const index = addMethods( | |
algoliasearch('latency', 'f4500fb6027c0b192d095289344542ba').initIndex( | |
'instant_search' | |
), | |
{ customBrowse } | |
); | |
function getEverything() { | |
let hits = []; | |
return index | |
.customBrowse({ | |
limit: '10000', | |
batch(batch) { | |
hits.push(...batch); | |
}, | |
}) | |
.then(() => hits); | |
} | |
getEverything().then(everything => console.log(everything.length)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment