Skip to content

Commit

Permalink
Change batch index loop
Browse files Browse the repository at this point in the history
-Switch to Array.forEach method for faster indexing
-Use lodash chunk method for batching
  • Loading branch information
ronitagarwala01 committed Nov 21, 2023
1 parent 263711e commit b0ea9ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { pathToFileURL } from 'url'
import { Client } from '@opensearch-project/opensearch'
import type { ClientOptions } from '@opensearch-project/opensearch'
import { exists } from './paths'
import _ from 'lodash'

const jsonFilename = 'sandbox-search.json'
const jsFilename = 'sandbox-search.js'
Expand Down Expand Up @@ -45,10 +46,9 @@ export async function populate(path: string, opts: ClientOptions) {
if (data) {
const client = new Client(opts)
const batch_size = 10
for (let i = 0; i < data.length; i += batch_size) {
const batch = data.slice(i, i + batch_size)
const batches = _.chunk(data, batch_size)
batches.forEach(async (batch: object[]) => {
await client.bulk({ body: batch })
console.log(`Indexed ${batch.length + i} records`)
}
})
}
}
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@opensearch-project/opensearch": "^2.2.0",
"env-paths": "^3.0.0",
"lodash": "^4.17.21",
"make-fetch-happen": "^11.0.3",
"rimraf": "^4.1.2",
"tar": "^6.1.13",
Expand Down

0 comments on commit b0ea9ac

Please sign in to comment.