2017-01-26 6 views
0

하위 문서의 속성이 변경된 경우 ElasticSearch에서 모든 상위 문서를 자동으로 업데이트하는 방법이 있습니까? 아마도 나는 ElasticSearch를 어떻게 사용하고 있는지 추적 할 수있는 방법 일 것입니다. 코드 :하위 문서에 대한 ElasticSearch 업데이트가 상위 문서를 업데이트하지 않음

자녀는 여러 다른 학부모에게 속할 수 있습니다. 자녀에 대한 변경 사항을 자녀를 포함하는 모든 학부모에게 전파 할 수있는 방법이 있습니까? C#에서 Nest 클라이언트를 사용하고 있습니다.

답변

1

당신이하고있는 일은 옳지 않습니다. 매핑에서

  1. , 당신은 그 인덱스 parentchild 모두 중첩 형식으로 Child 속성을 설정하지만.

    중첩 형은이 parentChild의 속성을 나타내는 JSON 상위 JSON 문서의 일부분으로 인덱싱되며, 그 중첩 를되는 유형에 연동된다.

    하나의 부모가 많은 자식이있는 Elasticsearch에 Parent/Child relationship이있을 수 있습니다. 사용하려는 모델 내에서 부모/자식 역할을 뒤집을 필요가있는 것처럼 들립니다.

  2. 색인에 따라, 당신은 parent 문서의 소스를 얻을, 부모에 대한 자식의 자식의 이름을 변경할 수 있지만 다음 parent아이로를 업데이트없이 당신이 색인을 하위 문서, 를 업데이트합니다.

    많은 문서는 동일한 중첩 된 문서 값을 가질 수 있지만 이러한 문서간에 관계가 없으므로 값을 업데이트하려면 각 문서에 대한 업데이트가 필요합니다. 이것은 Update By Query API으로 할 수 있습니다. 여기

는 입증하기 예이다; 생산에, 당신은 아마 등

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; } 
} 
+0

감사를 직접 스트리밍을 사용하지 않도록 모든 요청/응답 로그 아웃, 각 작업 후 새로 고침을 호출하고 싶지 않아, 그 작동 ... 때 아이가 할 수있는 자사의 지저분한 부모의 다른 유형에 속해 있습니다 ... 클라이언트가 프로젝트, 주문 등에 속할 수있는 것처럼 – user10479

+0

Elasticsearch는 검색, 분석 및 대규모 데이터를 훌륭한 성능으로 처리하는 데 매우 적합합니다. 그러나 관계형 데이터 저장소는 아니므로 데이터를 모델링하는 방법을 다시 설계해야 할 수 있습니다. –