elasticsearch 嵌套Elasticsearch重新索引示例,elasticsearch,nest,elasticsearch,Nest" /> elasticsearch 嵌套Elasticsearch重新索引示例,elasticsearch,nest,elasticsearch,Nest" />

elasticsearch 嵌套Elasticsearch重新索引示例

elasticsearch 嵌套Elasticsearch重新索引示例,elasticsearch,nest,elasticsearch,Nest,我的目标是用1000万个碎片重新索引索引,以便更改字段映射,以便于进行重要项分析 我的问题是,我在使用NEST库执行重新索引时遇到问题,而且文档(非常)有限。如果可能,我需要一个使用中的以下示例: NEST提供了一个很好的Reindex方法,您可以使用它,尽管缺少文档。我已经在这个特别的WinForms代码中粗略地使用了它 private ElasticClient client; private double count; private void reindex_

我的目标是用1000万个碎片重新索引索引,以便更改字段映射,以便于进行重要项分析

我的问题是,我在使用NEST库执行重新索引时遇到问题,而且文档(非常)有限。如果可能,我需要一个使用中的以下示例:


NEST提供了一个很好的
Reindex
方法,您可以使用它,尽管缺少文档。我已经在这个特别的WinForms代码中粗略地使用了它

    private ElasticClient client;
    private double count;

    private void reindex_Completed()
    {
        MessageBox.Show("Done!");
    }

    private void reindex_Next(IReindexResponse<object> obj)
    {
        count += obj.BulkResponse.Items.Count();
        var progress = 100 * count / (double)obj.SearchResponse.Total;
        progressBar1.Value = (int)progress;
    }

    private void reindex_Error(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

    private void button1_Click(object sender, EventArgs e)
    {
        count = 0;

        var reindex = client.Reindex<object>(r => r.FromIndex(fromIndex.Text).NewIndexName(toIndex.Text).Scroll("10s"));

        var o = new ReindexObserver<object>(onError: reindex_Error, onNext: reindex_Next, completed: reindex_Completed);
        reindex.Subscribe(o);
    }
私人ElasticClient;
私人重复计数;
私有无效重新索引已完成()
{
MessageBox.Show(“完成!”);
}
私有无效重新索引(IReindexResponse obj)
{
count+=obj.BulkResponse.Items.count();
var progress=100*计数/(双)obj.SearchResponse.Total;
progressBar1.Value=(int)进度;
}
私有void重新索引错误(异常ex)
{
Show(例如ToString());
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
计数=0;
var reindex=client.reindex(r=>r.FromIndex(FromIndex.Text).NewIndexName(toIndex.Text).Scroll(“10s”);
var o=新的重新索引观察者(onError:reindex_Error,onNext:reindex_Next,completed:reindex_completed);
重新索引订阅(o);
}

我刚刚找到一篇博客文章,它向我展示了如何做到这一点:

不幸的是,
NEST
的实现并不是我所期望的。在我看来,对于可能是最常见的用例来说,它的设计有点过头了

很多人只是想用零停机时间更新他们的映射

在我的例子中,我已经注意到创建索引及其所有设置和映射,但是
NEST
坚持在重新编制索引时必须创建一个新索引。除此之外,还有许多其他事情。其他事情太多了

我发现直接实现要简单得多——因为NEST已经有了
搜索
滚动
、和
批量
方法。(这是从
NEST
的实现中采用的):


我赞同本·王尔德的上述回答。最好完全控制索引创建和重新索引过程

Ben的代码缺少的是对父/子关系的支持。以下是我的代码来解决这个问题:

更换以下线路:

foreach (var hit in searchResult.Hits)
{
    b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
}
foreach(searchResult.Hits中的var hit)
{
b、 索引(bi=>bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
}
为此:

foreach (var hit in searchResult.Hits)
{
    var jo = hit.Source as JObject;
    JToken jt;
    if(jo != null && jo.TryGetValue("parentId", out jt))
    {
        // Document is child-document => add parent reference
        string parentId = (string)jt;
        b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id).Parent(parentId));
    }
    else
    {
        b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
    }                                
}
foreach(searchResult.Hits中的var hit)
{
var jo=hit.Source作为JObject;
jtokenjt;
if(jo!=null&&jo.TryGetValue(“parentId”,out jt))
{
//文档是子文档=>添加父引用
字符串parentId=(字符串)jt;
b、 索引(bi=>bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id).parentId));
}
其他的
{
b、 索引(bi=>bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
}                                
}

非常感谢,我会看看我能用它做些什么!对我来说最重要的是调用reindex.Subscribe()并观察异常被抛出。这对我来说是无声失败的,原因有两个:1)索引已经存在,2)当前索引不包含任何文档。如果我没有在reindex()调用中创建新索引,则会引发reindexception,原因是什么?或者尝试关注此博客,现在可以通过NEST使用服务器端reindex。
foreach (var hit in searchResult.Hits)
{
    b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
}
foreach (var hit in searchResult.Hits)
{
    var jo = hit.Source as JObject;
    JToken jt;
    if(jo != null && jo.TryGetValue("parentId", out jt))
    {
        // Document is child-document => add parent reference
        string parentId = (string)jt;
        b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id).Parent(parentId));
    }
    else
    {
        b.Index<object>(bi => bi.Document(hit.Source).Type(hit.Type).Index(nextIndexName).Id(hit.Id));
    }                                
}