Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { Client } from '@elastic/elasticsearch';
import type { BulkResponse } from '@elastic/elasticsearch/lib/api/types';
import { ToolingLog } from '@kbn/tooling-log';
import { v4 } from 'uuid';

interface IndexDocumentsParams {
es: Client;
Expand All @@ -22,7 +23,10 @@ type IndexDocuments = (params: IndexDocumentsParams) => Promise<BulkResponse>;
* Indexes documents into provided index
*/
export const indexDocuments: IndexDocuments = async ({ es, documents, index, log }) => {
const operations = documents.flatMap((doc: object) => [{ index: { _index: index } }, doc]);
const operations = documents.flatMap(({ _id, ...doc }: Record<string, unknown>) => [
{ index: { _index: index, _id: _id ?? v4() } },
doc,
]);

const response = await es.bulk({ refresh: true, operations });

Expand Down