2016-09-27 138 views
0

說,我有一些POCO像下面。嵌套for elasticsearch版本2.0.0.0嵌套對象的部分更新

public class Graph 
{ 
    public string Id { get; set; } // Indexed by this 
    public List<Node> NodeList { get; set; } 
} 

public class Node 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public List<Edge> EdgeList { get; set; } 
} 

public class Edge 
{ 
    public string Id { get; set; } 
    public double Cost { get; set; } 
} 

當部分更新我的Graph 我想這是Id找到NodeList現有Node,並更新它的NameEdge財產。我不要想要在我的NodeList中添加新的Node對象。只想更新現有的。

SOFAR我已經試過:

public void UpdateGraph(string index, Graph graph) 
{ 
    var docPath = new DocumentPath<Graph>(graph.Id).Index(index); 
    try 
    { 
     var updateResp = client.Update<Graph, Graph>(docPath, searchDescriptor => searchDescriptor 
      .Doc(graph)  
      .RetryOnConflict(4) 
      .Refresh(true) 
     ); 
    } 
} 

在我目前的執行情況,你可以看到所有我做的是更換 老Graph對象。但我想部分更新我的Graph對象。我想發送Node對象列表作爲參數, 查找NodeList中的那些對象,只更新那些對象。

也許有些東西像下面,

public void UpdateGraph(string index, List<Node> node) 
{ 
    //Code here 
} 

回答

1

因爲NodeListList<Node>,部分更新是不可能的,因爲提供的值將取代現有的值。

但是,您可以使用optimistic concurrency control

  1. 獲得現有文檔
  2. 使您的應用程序
  3. 指數改變的文件的更改回Elasticsearch,使用的版本號從樂觀的GET請求併發

像下面的內容將工作

var getResponse = client.Get<Graph>("graph-id"); 

var graph = getResponse.Source; 
var node = graph.NodeList.First(n => n.Id == "node-id"); 

// make changes to the node 
node.Name = "new name"; 
node.EdgeList.First().Cost = 9.99; 

var indexResponse = client.Index(graph, i => i 
    // specify the version from the get request 
    .Version(getResponse.Version) 
); 

如果Graph在獲取和索引調用之間進行了更改,則索引調用將返回409響應。

如果你經常需要更新Node S和Edge獨立自主地彼此,你可能會決定他們Parent/Child relationships這將讓你無需拉回來對象圖和指數的變化更新模型。