/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import { Readable as ReadableStream } from 'stream'; import { TransportRequestOptions, ApiError, ApiResponse, RequestBody } from './Transport'; import { Search_Request } from '../api'; import { Msearch_Request } from '../api'; import { Bulk_Request } from '../api'; export default class Helpers { search( params: Search_Request, options?: TransportRequestOptions ): Promise; scrollSearch< TDocument = unknown >( params: Search_Request, options?: TransportRequestOptions ): AsyncIterable>; scrollDocuments( params: Search_Request, options?: TransportRequestOptions ): AsyncIterable; msearch(options?: MsearchHelperOptions, reqOptions?: TransportRequestOptions): MsearchHelper; bulk( options: BulkHelperOptions, reqOptions?: TransportRequestOptions ): BulkHelper; } export interface ScrollSearchResponse< TDocument = unknown > extends ApiResponse { clear: () => Promise; documents: TDocument[]; } export interface BulkHelper extends Promise { abort: () => BulkHelper; readonly stats: BulkStats; } export interface BulkStats { total: number; failed: number; retry: number; successful: number; noop: number; time: number; bytes: number; aborted: boolean; } interface IndexActionOperation { index: { _index: string; [key: string]: any; }; } interface CreateActionOperation { create: { _index: string; [key: string]: any; }; } interface UpdateActionOperation { update: { _index: string; [key: string]: any; }; } interface DeleteAction { delete: { _index: string; [key: string]: any; }; } type CreateAction = CreateActionOperation | [CreateActionOperation, unknown]; type IndexAction = IndexActionOperation | [IndexActionOperation, unknown]; type UpdateAction = [UpdateActionOperation, Record]; type Action = IndexAction | CreateAction | UpdateAction | DeleteAction; type Omit = Pick>; export interface BulkHelperOptions extends Omit { datasource: TDocument[] | Buffer | ReadableStream | AsyncIterator; onDocument: (doc: TDocument) => Action; flushBytes?: number; flushInterval?: number; concurrency?: number; retries?: number; wait?: number; onDrop?: (doc: OnDropDocument) => void; refreshOnCompletion?: boolean | string; } export interface OnDropDocument { status: number; error: { type: string; reason: string; caused_by: { type: string; reason: string; }; }; operation: Action; document: TDocument; retried: boolean; } export interface MsearchHelperOptions extends Omit { operations?: number; flushInterval?: number; concurrency?: number; retries?: number; wait?: number; } declare type callbackFn = ( err: ApiError, result: ApiResponse ) => void; export interface MsearchHelper extends Promise { stop(error?: Error): void; search< TResponse = Record, TRequestBody extends RequestBody = Record >( header: Omit, body: TRequestBody ): Promise; search< TResponse = Record, TRequestBody extends RequestBody = Record >( header: Omit, body: TRequestBody, callback: callbackFn ): void; }