Nest - 重建索引

Nest - Reindexing

Elasticsearch 在 Elasticsearch 2.3.0 中发布了新的 Reindex API,当前版本的 NEST (2.1.1) 是否使用了这个 api?如果没有,是否有计划这样做? 我知道当前版本有一个 reindex 方法,但它会强制您创建新索引。对于我的用例,索引已经存在。

任何feedback/insights将得到极大的应用。谢谢!

这种问题最好在 github issues for NEST 上提出,因为项目的提交者将能够最好地回答 :)

A commit went in on 6 April to map the new Reindex API available in Elasticsearch 2.3.0, along with other features like the Task Management API and Update By Query. This made its way into NEST 2.3.0

NEST 2.x 已经包含一个用于重建索引的助手,它在幕后使用 scan/scroll 和 returns 一个 IObservable<IReindexResponse<T>> 可用于观察进度

public class Document {}

var observable = client.Reindex<Document>("from-index", "to-index", r => r
    // settings to use when creating to-index
    .CreateIndex(c => c
        .Settings(s => s
            .NumberOfShards(5)
            .NumberOfReplicas(2)
        )
    )
    // query to optionally limit documents re-indexed from from-index to to-index
    .Query(q => q.MatchAll())
    // the number of documents to reindex in each request.
    // NOTE: The number of documents in each request will actually be
    //     NUMBER * NUMBER OF SHARDS IN from-index
    // since reindex uses scan/scroll
    .Size(100)
);

ExceptionDispatchInfo e = null;
var waitHandle = new ManualResetEvent(false);

var observer = new ReindexObserver<Document>(
    onNext: reindexResponse =>
    {
        // do something with notification. Maybe log total progress
    },
    onError: exception =>
    {
        e = ExceptionDispatchInfo.Capture(exception);
        waitHandle.Set();
    },
    completed: () =>
    {
        // Maybe log completion, refresh the index, etc..
        waitHandle.Set();
    }
);

observable.Subscribe(observer);  

// wait for the handle to be signalled
waitHandle.Wait();

// throw the exception if one was captured
e?.Throw();

查看 ReIndex API tests 以获得一些想法。

新的 Reindex API 在客户端中被命名为 client.ReIndexOnServer() 以区别于现有的可观察实现。