Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ElasticSearch更新到子文档而不更新父文档_C#_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Nest - Fatal编程技术网 elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

C# ElasticSearch更新到子文档而不更新父文档

C# ElasticSearch更新到子文档而不更新父文档,c#,elasticsearch,nest,C#,elasticsearch,Nest,ElasticSearch中是否有一种方法可以在子文档的属性发生更改时自动更新所有父文档?也许我正在追踪我是如何使用ElasticSearch的。守则: var child = new Child { Id = Guid.NewGuid(), Name = "Child" }; var parent = new Parent { Id = Gu

ElasticSearch中是否有一种方法可以在子文档的属性发生更改时自动更新所有父文档?也许我正在追踪我是如何使用ElasticSearch的。守则:

        var child = new Child
        {
            Id = Guid.NewGuid(),
            Name = "Child"
        };

        var parent = new Parent
        {
            Id = Guid.NewGuid(),
            Name = "Parent",
            Child = child
        };

        var nestedResponse = client.CreateIndex("index", i => i
            .Mappings(m => m
                .Map<Parent>(map => map
                    .AutoMap()
                    .Properties(ps => ps
                        .Nested<Child>(n => n
                            .Name(p => p.Child)
                            .AutoMap()
                        )
                    )
                )
            )
        );

        var indexResult = client.Index<Parent>(parent);
        indexResult = client.Index<Child>(child);

        var reloadedParent = client.Get<Parent>(parent.Id.ToString()).Source;
        var childName = reloadedParent.Child.Name;
        child.Name = "child changed";
        var updateRequest = new UpdateRequest<Child, Child>("index", typeof(Child), child.Id);
        updateRequest.Doc = child;

        var reindexResult = client.Update<Child>(updateRequest);

        var reloadedParentAfterChildChange = client.Get<Parent>(parent.Id.ToString()).Source;
        var childChangedName = reloadedParentAfterChildChange.Child.Name;

        Assert.AreEqual(child.Name, childChangedName);

    }
}

public class Parent
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Child Child { get; set; }
}

public class Child
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

这个孩子可能属于许多不同的父母。有没有办法将对子项的更改传播到包含该子项的所有父项?我正在使用c中的Nest客户端。

您所做的不太正确

在映射中,您将子属性设置为嵌套类型,但随后索引父属性和子属性

嵌套类型根据其嵌套的类型编制索引,也就是说,表示父对象上的子属性的json作为父json文档的一部分编制索引

在Elasticsearch中,一个父对象对多个子对象是可能的,这听起来像是需要反转模型中的父/子角色才能使用

编制索引后,您将获取父文档的源,更改父文档中子文档的子名称,然后更新已编制索引的子文档,而不使用子文档更新父文档

许多文档可以具有相同的嵌套文档值,但这些文档之间没有关系,因此更新这些值需要对每个文档进行更新。这可以通过

这里有一个例子来说明;在生产环境中,您可能不想禁用直接流、注销所有请求/响应、在每次操作后刷新调用等

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "default-index";
    var connectionSettings = new ConnectionSettings(pool)
            .DefaultIndex(defaultIndex)
            .PrettyJson()
            .DisableDirectStreaming()
            .OnRequestCompleted(response =>
                {
                    // log out the request
                    if (response.RequestBodyInBytes != null)
                    {
                        Console.WriteLine(
                            $"{response.HttpMethod} {response.Uri} \n" +
                            $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                    }
                    else
                    {
                        Console.WriteLine($"{response.HttpMethod} {response.Uri}");
                    }

                    Console.WriteLine();

                    // log out the response
                    if (response.ResponseBodyInBytes != null)
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                    else
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                });

    var client = new ElasticClient(connectionSettings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    var child = new Child
    {
        Id = Guid.NewGuid(),
        Name = "Child"
    };

    var parent = new Parent
    {
        Id = Guid.NewGuid(),
        Name = "Parent",
        Child = child
    };

    var anotherParent = new Parent
    {
        Id = Guid.NewGuid(),
        Name = "Another Parent",
        Child = child
    };

    var nestedResponse = client.CreateIndex(defaultIndex, i => i
        .Mappings(m => m
            .Map<Parent>(map => map
                .AutoMap()
                .Properties(ps => ps
                    .String(s => s
                        .Name(nn => nn.Id)
                        .NotAnalyzed()
                    )
                    .Nested<Child>(n => n
                        .Name(p => p.Child)
                        .AutoMap()
                        .Properties(p => p
                            .String(s => s
                                .Name(nn => nn.Id)
                                .NotAnalyzed()
                            )
                        )
                    )
                )
            )
        )
    );

    var indexResult = client.Index<Parent>(parent);
    indexResult = client.Index<Parent>(anotherParent);

    var fetchedParent = client.Get<Parent>(parent.Id).Source;
    var fetchedAnotherParent = client.Get<Parent>(anotherParent.Id).Source;

    client.Refresh(defaultIndex);

    var update = client.UpdateByQuery<Parent>(u => u
        .Query(q => q
            .Nested(n => n
                .Path(p => p.Child)
                .Query(qq => qq
                    .Term(t => t.Child.Id, child.Id)
                )
            )
        )
        .Script("ctx._source.child.name='New Child Name'")
        .Conflicts(Conflicts.Abort)
        .WaitForCompletion()
        .Refresh()
    );

    fetchedParent = client.Get<Parent>(parent.Id).Source;
    fetchedAnotherParent = client.Get<Parent>(anotherParent.Id).Source;
}


public class Parent
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public Child Child { get; set;} 
}

public class Child
{
    public Guid Id { get; set; }

    public string Name { get; set; } 
}

谢谢,这很有效。。。当孩子可能属于不同类型的父母时,这有点混乱。。。就像一个客户可以属于一个项目一样,order etcElasticsearch非常适合大规模搜索、分析和处理大数据,并具有出色的性能。但是,它不是关系数据存储,因此可能需要重新设计数据建模的方式。