gpt4 book ai didi

Elasticsearch - NEST - Upsert

转载 作者:行者123 更新时间:2023-12-03 01:23:24 26 4
gpt4 key购买 nike

我有以下两个类(class)

条目

public class Entry
{
public Guid Id { get; set; }
public IEnumerable<Data> Data { get; set; }
}

入口数据
public class EntryData
{
public string Type { get; set; }
public object Data { get; set; }
}

我有一堆不同的应用程序将消息生成到一个队列中,然后我在一个单独的应用程序中使用这些消息以将这些数据存储在 elasticsearch 中。

我为所有消息使用 CorrelationId,我想将此 ID 用作 Elasticsearch 中的 ID。
所以给出以下数据:
var id = Guid.Parse("1befd5b62b944b4aa600c85632159e11");
var entries = new List<Entry>
{
new Entry
{
Id = id,
Data = new List<EntryData>
{
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION1_Received"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION1_Validated"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION1_Published"
},
}
},
new Entry
{
Id = id,
Data = new List<EntryData>
{
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION2_Received"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION2_Validated"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION2_Published"
},
}
},
new Entry
{
Id = id,
Data = new List<EntryData>
{
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION3_Received"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION3_Validated"
},
new EntryData
{
Data = DateTime.UtcNow,
Type = "APPLICATION3_Published"
},
}
},

};


我希望将其保存为 一项 在 Elasticsearch 中 ID == 1befd5b6-2b94-4b4a-a600-c85632159e11data包含 的数组9 个元素 .

在尝试以下操作时,我在努力让它发挥作用:
var result = await _elasticClient.BulkAsync(x => x.Index("journal").UpdateMany(entries, (descriptor, entry) => {
descriptor.Doc(entry);
return descriptor.Upsert(entry);
}), cancellationToken);

但这只是覆盖数据数组中已经存在的任何内容,并且计数是 3 而不是 9(仅保存 Application3 条目)。

那么,有没有可能做我想做的事?我以前从未使用过 elasticsearch,所以感觉好像我在这里遗漏了一些简单的东西...... :)

最佳答案

设法像这样解决它:

var result = await _elasticClient.BulkAsync(x => x.Index("journal").UpdateMany(entries, (descriptor, entry) => {
var script = new InlineScript("ctx._source.data.addAll(params.data)")
{
Params = new Dictionary<string, object> {{"data", entry.Data}}
};
descriptor.Script(b => script);
return descriptor.Upsert(entry);
}), cancellationToken);

关于Elasticsearch - NEST - Upsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com