/* 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.Threading;
namespace OpenSearch.Client
{
public partial interface IOpenSearchClient
{
///
/// Helper method that allows you to reindex from one index into another using ScrollAll and BulkAll.
///
/// An IObservable<ReindexResponse<T>$gt; you can subscribe to to listen to the progress of the reindex process
IObservable Reindex(
Func mapper,
Func, IReindexRequest> selector,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class
where TTarget : class;
///
/// Helper method that allows you to reindex from one index into another using ScrollAll and BulkAll.
///
/// An IObservable<ReindexResponse<T>$gt; you can subscribe to to listen to the progress of the reindex process
IObservable Reindex(
Func, IReindexRequest> selector,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class;
///
/// Helper method that allows you to reindex from one index into another using ScrollAll and BulkAll.
///
/// a request object to describe the reindex operation
/// An IObservable<ReindexResponse<T>$gt; you can subscribe to to listen to the progress of the reindex process
IObservable Reindex(
IReindexRequest request,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class
where TTarget : class;
///
/// Helper method that allows you to reindex from one index into another using ScrollAll and BulkAll.
///
/// a request object to describe the reindex operation
/// An IObservable<ReindexResponse<T>$gt; you can subscribe to to listen to the progress of the reindex process
IObservable Reindex(
IReindexRequest request,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class;
///
/// Simplified form for reindex which will cover 80% of its usecases. Allows you to index all documents of type T from
/// to
/// optionally limiting the documents found in by using .
///
/// The source index, from which all types will be returned
///
/// The target index, if it does not exist already will be created using the same settings of
///
///
/// an optional query limiting the documents found in
IObservable Reindex(
IndexName fromIndex,
IndexName toIndex,
Func mapper,
Func, QueryContainer> selector = null,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class
where TTarget : class;
///
/// Simplified form for reindex which will cover 80% of its use cases. Allows you to index all documents of type T from
/// to
/// optionally limiting the documents found in by using .
///
/// The source index, from which all types will be returned
///
/// The target index, if it does not exist already will be created using the same settings of
///
///
/// an optional query limiting the documents found in
IObservable Reindex(
IndexName fromIndex,
IndexName toIndex,
Func, QueryContainer> selector = null,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class;
}
public partial class OpenSearchClient
{
///
public IObservable Reindex(
Func, IReindexRequest> selector,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class =>
Reindex(selector.InvokeOrDefault(new ReindexDescriptor(s => s)));
///
public IObservable Reindex(
Func mapper,
Func, IReindexRequest> selector,
CancellationToken cancellationToken = default(CancellationToken)
)
where TTarget : class
where TSource : class =>
Reindex(selector.InvokeOrDefault(new ReindexDescriptor(mapper)));
///
public IObservable Reindex(
IReindexRequest request,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class =>
Reindex(request, cancellationToken);
///
public IObservable Reindex(
IReindexRequest request,
CancellationToken cancellationToken = default(CancellationToken)
)
where TTarget : class
where TSource : class
{
if (request.ScrollAll == null)
throw new ArgumentException(
"ScrollAll property must be set in order to get the source of a Reindex operation");
if (request.BulkAll == null)
throw new ArgumentException(
"BulkAll property must set in order to get the target of a Reindex operation");
return new ReindexObservable(this, ConnectionSettings, request, cancellationToken);
}
///
public IObservable Reindex(
IndexName fromIndex,
IndexName toIndex,
Func mapper,
Func, QueryContainer> selector = null,
CancellationToken cancellationToken = default(CancellationToken)
)
where TTarget : class
where TSource : class =>
Reindex(
mapper,
SimplifiedReindexer(fromIndex, toIndex, selector)
, cancellationToken);
///
public IObservable Reindex(
IndexName fromIndex,
IndexName toIndex,
Func, QueryContainer> selector = null,
CancellationToken cancellationToken = default(CancellationToken)
)
where TSource : class =>
Reindex(
s => s,
SimplifiedReindexer(fromIndex, toIndex, selector)
, cancellationToken);
private static Func, IReindexRequest> SimplifiedReindexer(
IndexName fromIndex,
IndexName toIndex,
Func, QueryContainer> selector
)
where TTarget : class
where TSource : class => r => r
.ScrollAll("1m", -1, search => search
.Search(ss => ss
.Size(CoordinatedRequestDefaults.ReindexScrollSize)
.Index(fromIndex)
.Query(selector)
)
)
.BulkAll(b => b.Index(toIndex));
}
}