簡體   English   中英

Elasticsearch NEST C# 客戶端 - 按條件批量更新嵌套列表(時間序列數據)

[英]Elasticsearch NEST C# Client - Bulk Partial update of a Nested List (time series data) by condition

我有一個索引結構,它具有一些普通屬性和嵌套列表屬性。 我需要根據使用Elasticsearch .NET 高級客戶端 NEST的條件更新(部分)批量/許多文檔嵌套時間序列列表。

示例數據

帶有日期和值的歷史列表是一個嵌套對象。 我只需要更新與某些日期匹配的值。 例如,我只想更新 historydate 2020-02-01 值而不是 history 中的其他 historydates 值 我知道我們可以實現完整的批量文檔索引和完整的嵌套列表替換,但我不知道部分嵌套列表。 我在任何地方都找不到 NEST 文檔。 有人可以幫助我。

[
  {
    "id": 1,
    "description": "market 1",
    "salesprice": 10.9,
    "cost": 7.95957,
    "history": [
      {
        "historydate": "2020-01-01",
        "quantity": 1
      },
      {
        "historydate": "2020-02-01",
        "quantity": 2
      },
      {
        "historydate": "2020-04-01",
        "quantity": 5
      }
    ]
  },
  {
    "id": 2,
    "description": "market 2",
    "salesprice": 12,
    "cost": 12,
    "history": [
      {
        "historydate": "2020-01-01",
        "quantity": 1
      },
      {
        "historydate": "2020-02-01",
        "quantity": 2
      },
      {
        "historydate": "2020-03-01",
        "quantity": 6
      },
    ]
  },
  {
    "id": 3,
    "description": "market3",
    "salesprice": 15,
    "cost": 15,
    "history": [
      {
        "historydate": "2020-01-01",
        "quantity": 10
      },
      {
        "historydate": "2020-02-01",
        "quantity": 20
      },
      {
        "historydate": "2020-03-01",
        "quantity": 30
      }
    ]
  }
]

我真的很感謝你的幫助。

謝謝你,克里希納

使用腳本,它必須解析每個匹配的文檔,但它會起作用。 注意:day 是我這邊的關鍵字,如果這是一個日期(可能添加類 + 映射),您將不得不閱讀一些簡單的文檔。

var result = Client.UpdateByQuery<yourClass>(u => u
                                        .Index(index)
                                        .Query(r => r.Term(tt => //your query)
                                        .Script(ss
                                            => ss.Source("for (item in ctx._source.history) {if (item.historyDate == params.day) { item.quantity = params.qty;} }")
                                                    .Params(new Dictionary<string, object>()
                                                        {
                                                            {"day", day},
                                                            {"qty", newQty }
                                                        }).Lang("painless"))

                                        .Conflicts(Net.Conflicts.Proceed)
                                        .Refresh(true) //optional
                      );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM