/* * 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, Context } from './Transport' import { Search, Msearch, Bulk } from '../api/requestParams' export default class Helpers { search>(params: Search, options?: TransportRequestOptions): Promise scrollSearch, TRequestBody extends RequestBody = Record, TContext = Context>(params: Search, options?: TransportRequestOptions): AsyncIterable> scrollDocuments>(params: Search, options?: TransportRequestOptions): AsyncIterable msearch(options?: MsearchHelperOptions, reqOptions?: TransportRequestOptions): MsearchHelper bulk(options: BulkHelperOptions, reqOptions?: TransportRequestOptions): BulkHelper } export interface ScrollSearchResponse, TContext = Context> extends ApiResponse { clear: () => Promise documents: TDocument[] } export interface BulkHelper extends Promise { abort: () => BulkHelper } export interface BulkStats { total: number failed: number retry: number successful: number time: number bytes: number aborted: boolean } interface IndexAction { index: { _index: string [key: string]: any } } interface CreateAction { create: { _index: string [key: string]: any } } interface UpdateActionOperation { update: { _index: string [key: string]: any } } interface DeleteAction { delete: { _index: string [key: string]: any } } 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 } } 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, TRequestBody extends RequestBody = Record, TContext = Context>(header: Omit, body: TRequestBody): Promise> search, TRequestBody extends RequestBody = Record, TContext = Context>(header: Omit, body: TRequestBody, callback: callbackFn): void }