gpt4 book ai didi

python - 将python代码中列表中的doc计数添加到elasticsearch中的字段中

转载 作者:行者123 更新时间:2023-12-02 22:21:14 25 4
gpt4 key购买 nike

我需要在 Elasticsearch 中更新文档的一个字段,并在 python 代码内的列表中添加该文档的计数。 weight字段包含数据集中文档的计数。数据集需要不时更新。因此每个文档的计数也必须更新。 hashed_ids是新批次数据中的文档 ID 列表。 weight匹配 id 的数量必须增加 hashed_ids 中该 id 的计数.
我尝试了下面的代码,但它不起作用。

hashed_ids = [hashlib.md5(doc.encode('utf-8')).hexdigest() for doc in shingles]
update_with_query_body = {
"script": {
"source": "ctx._source.content_completion.weight +=param.count",
"lang": "painless",
"param": {
"count": hashed_ids.count("ctx.['_id']")
}
},
"query": {
"ids": {
"values": hashed_ids
}
}
}
例如让我们说一个文档 id = d1b145716ce1b04ea53d1ede9875e05aweight =5 已经存在于索引中。还有字符串 d1b145716ce1b04ea53d1ede9875e05ahashed_ids 中重复了 3 次所以 update_with_query上面显示的查询将匹配数据库中的文档。我需要将 3 添加到 5 并且将 8 作为最终 weight

最佳答案

我不知道 python 但这里有一个例如基于一些假设的解决方案。
假设以下是 hashed_ids提取:

hashed_ids = ["id1","id1","id1","id2"]
要在术语查询中使用它,我们可以获得唯一的 id 列表,即
hashed_ids_unique = ["id1", "id2"]
让我们假设 doc(s) 是用以下结构索引的:
PUT test/_doc/1
{
"id": "id1",
"weight":9
}
现在我们可以按查询使用更新,如下所示:
POST test/_update_by_query
{
"query":{
"terms": {
"id":["id1","id2"]
}
},
"script":{
"source":"long weightToAdd = params.hashed_ids.stream().filter(idFromList -> ctx._source.id.equals(idFromList)).count(); ctx._source.weight += weightToAdd;",
"params":{
"hashed_ids":["id1","id1","id1","id2"]
}
}
}
脚本说明:
以下给出了 hashed_ids 中匹配 id 的计数 id 的列表当前匹配的文档。
long weightToAdd = params.hashed_ids.stream().filter(idFromList -> ctx._source.id.equals(idFromList)).count();
以下相加 weightToAddweight 的现有值在文件中。
ctx._source.weight += weightToAdd;

关于python - 将python代码中列表中的doc计数添加到elasticsearch中的字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62495168/

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