/// <summary> /// Execute bulk index, create, update, or delete actions. This is the simpler version which /// assumes you are only performing a single action type on a single index and document type. /// </summary> /// <param name="index">The index and type.</param> /// <param name="type">The document type.</param> /// <param name="actionType">Type of the action.</param> /// <param name="documents">The documents, if <see cref="BulkActionType.Index" /> or <see cref="BulkActionType.Create" /> /// is specified, this is the source document to index or create, if <see cref="BulkActionType.Update" /> /// is specified this should be an update statement, and if <see cref="BulkActionType.Delete" /> is specified /// this is either the ID or a document which implements <see cref="IKeyDocument" />.</param> /// <param name="options">Any options for the request such as 'refresh'.</param> /// <param name="throwOnFailure">If true, an exception will be thrown if any of the requested actions fail. (Normally the _bulk /// API endpoint returns a 200 if the request was processed successfully, even if document actions fail.)</param> /// <param name="cancel">A cancellation token for the request.</param> /// <returns></returns> public Task <BulkActionResult> BulkActionAsync(string index, string type, BulkActionType actionType, IEnumerable <object> documents, object options = null, bool throwOnFailure = true, CancellationToken cancel = default(CancellationToken)) { if (index == null) { throw new ArgumentNullException("An index is required", nameof(index)); } // No punishment for providing null/empty requests if (documents?.Any() != true) { return(Task.FromResult(new BulkActionResult { Took = TimeSpan.Zero, HasErrors = false, Items = Array.Empty <BulkActionResultItem>() })); } var actionName = actionType.ToString().ToLower(); var request = new StringBuilder(); foreach (var document in documents) { if (actionType == BulkActionType.Delete) { var key = (document as IKeyDocument)?.Key ?? document ?? throw new ArgumentException("No key was provided for delete operation", nameof(documents)); var requestItem = new BulkActionRequest(actionType) { ID = key }; request.Append(JsonConvert.SerializeObject(requestItem, _jsonSettings)); } else { var requestItem = new BulkActionRequest(actionType) { ID = (document as IKeyDocument)?.Key, Document = document }; request.Append(JsonConvert.SerializeObject(requestItem, _jsonSettings)); } } var requestUri = new Uri(_hostProvider.Next() + $"{index}/{type}/_bulk{QueryStringParser.GetQueryString(options)}"); return(MakeRequestAsync <BulkActionResult>( HttpMethod.Post, requestUri, request.ToString(), MediaTypes.ApplicationNewlineDelimittedJson, cancel, "bulk_action")); }
/// <summary> /// Represents a request for a document action within a bulk action request. /// </summary> /// <param name="action">The action to perform.</param> public BulkActionRequest(BulkActionType action) { Action = action; }