/* 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. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * 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. */ using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using OpenSearch.Net.Utf8Json; namespace OpenSearch.Client { [JsonFormatter(typeof(BulkRequestFormatter))] [MapsApi("bulk.json")] public partial interface IBulkRequest { [IgnoreDataMember] BulkOperationsCollection Operations { get; set; } } public partial class BulkRequest { public BulkOperationsCollection Operations { get; set; } } public partial class BulkDescriptor { BulkOperationsCollection IBulkRequest.Operations { get; set; } = new BulkOperationsCollection(); public BulkDescriptor Create(Func, IBulkCreateOperation> bulkCreateSelector) where T : class => AddOperation(bulkCreateSelector?.Invoke(new BulkCreateDescriptor())); /// /// CreateMany, convenience method to create many documents at once. /// /// the objects to create /// A func called on each object to describe the individual create operation public BulkDescriptor CreateMany(IEnumerable @objects, Func, T, IBulkCreateOperation> bulkCreateSelector = null) where T : class => AddOperations(@objects, bulkCreateSelector, o => new BulkCreateDescriptor().Document(o)); public BulkDescriptor Index(Func, IBulkIndexOperation> bulkIndexSelector) where T : class => AddOperation(bulkIndexSelector?.Invoke(new BulkIndexDescriptor())); /// /// IndexMany, convenience method to pass many objects at once. /// /// the objects to index /// A func called on each object to describe the individual index operation public BulkDescriptor IndexMany(IEnumerable @objects, Func, T, IBulkIndexOperation> bulkIndexSelector = null) where T : class => AddOperations(@objects, bulkIndexSelector, o => new BulkIndexDescriptor().Document(o)); /// /// DeleteMany, convenience method to delete many objects at once. /// /// the objects to delete /// A func called on each object to describe the individual delete operation public BulkDescriptor DeleteMany( IEnumerable @objects, Func, T, IBulkDeleteOperation> bulkDeleteSelector = null ) where T : class => AddOperations(@objects, bulkDeleteSelector, o => new BulkDeleteDescriptor().Document(o)); /// /// DeleteMany, convenience method to delete many objects at once. /// /// Enumerable of string ids to delete /// A func called on each ids to describe the individual delete operation public BulkDescriptor DeleteMany( IEnumerable ids, Func, string, IBulkDeleteOperation> bulkDeleteSelector = null ) where T : class => AddOperations(ids, bulkDeleteSelector, id => new BulkDeleteDescriptor().Id(id)); /// /// DeleteMany, convenience method to delete many objects at once. /// /// Enumerable of int ids to delete /// A func called on each ids to describe the individual delete operation public BulkDescriptor DeleteMany( IEnumerable ids, Func, long, IBulkDeleteOperation> bulkDeleteSelector = null ) where T : class => AddOperations(ids, bulkDeleteSelector, id => new BulkDeleteDescriptor().Id(id)); public BulkDescriptor Delete(T obj, Func, IBulkDeleteOperation> bulkDeleteSelector = null) where T : class => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor().Document(obj))); public BulkDescriptor Delete(Func, IBulkDeleteOperation> bulkDeleteSelector) where T : class => AddOperation(bulkDeleteSelector?.Invoke(new BulkDeleteDescriptor())); /// /// UpdateMany, convenience method to pass many objects at once to do multiple updates. /// /// the objects to update /// An func called on each object to describe the individual update operation public BulkDescriptor UpdateMany( IEnumerable @objects, Func, T, IBulkUpdateOperation> bulkUpdateSelector ) where T : class => AddOperations(objects, bulkUpdateSelector, o => new BulkUpdateDescriptor().IdFrom(o)); /// /// UpdateMany, convenience method to pass many objects at once to do multiple updates. /// /// the objects to update /// An func called on each object to describe the individual update operation public BulkDescriptor UpdateMany( IEnumerable @objects, Func, T, IBulkUpdateOperation> bulkUpdateSelector ) where T : class where TPartialDocument : class => AddOperations(objects, bulkUpdateSelector, o => new BulkUpdateDescriptor().IdFrom(o)); public BulkDescriptor Update(Func, IBulkUpdateOperation> bulkUpdateSelector) where T : class => Update(bulkUpdateSelector); public BulkDescriptor Update( Func, IBulkUpdateOperation> bulkUpdateSelector ) where T : class where TPartialDocument : class => AddOperation(bulkUpdateSelector?.Invoke(new BulkUpdateDescriptor())); public BulkDescriptor AddOperation(IBulkOperation operation) => Assign(operation, (a, v) => a.Operations.AddIfNotNull(v)); private BulkDescriptor AddOperations( IEnumerable objects, Func bulkIndexSelector, Func defaultSelector ) where TInterface : class, IBulkOperation where TDescriptor : class, TInterface { if (@objects == null) return this; var objectsList = @objects.ToList(); var operations = new List(objectsList.Count()); foreach (var o in objectsList) { var op = bulkIndexSelector.InvokeOrDefault(defaultSelector(o), o); if (op != null) operations.Add(op); } return Assign(operations, (a, v) => a.Operations.AddRange(v)); } } }