例如,我們在彈性中有以下檔案:
{
"name": "Bob",
"age": "22",
"phrase": "ohohoho",
"date": "2022-10-20T00:00:00Z"
}
字串短語;
約會時間?日期;
然后我們要放置以下內容:
{
"name": "not Bob",
"age": "22",
"phrase": null,
"date": null
}
在 C# 中:
var updateRequest = new UpdateRequest<T, T>(entity)
{
ScriptedUpsert = true,
Script = new InlineScript(
$"if (someCondition) {{ctx._source.putAll(params.entity);}} else {{ctx.op = \"noop\";}}")
{
Lang = "painless",
Params = new Dictionary<string, object>() { { "entity", entity } },
},
Upsert = Activator.CreateInstance<T>()
};
但最終它不會更新短語和日期。它提出以下要求:
POST /myIndex/_update/b90278fd-1a66-40bf-b775-d076122c6c02
{
"script": {
"source": ""if (someCondition) {{ctx._source.putAll(params.entity);}} else {{ctx.op = \"noop\";}}"",
"lang": "painless",
"params": {
"entity": {
"name": "",
"age": 22
}
}
},
"upsert": {
"age": 0
}
}
不知道為什么,但它會跳過所有帶有 null 的欄位。如何將可空欄位更新為空?
uj5u.com熱心網友回復:
NEST 默認不支持發送空值。
您可以有一個簽入腳本,這樣如果一個值沒有通過,那么您可以將它從檔案中洗掉。
var updateRequest = new UpdateRequest<T, T(entity)
{
ScriptedUpsert = true,
Script = new InlineScript($"if (params.entity.phrase==null)ctx._source.remove('phrase');")
{
Lang = "painless",
Params = new Dictionary<string, object>() { { "entity", entity } },
},
Upsert = Activator.CreateInstance<T>()
};
您可以在此處查看更多詳細資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/523551.html
標籤:C#弹性搜索巢
