elasticsearch - 当文档不存在时,Upsert 不会添加新的(ElasticSearch)
问题描述
我正在创建这样的新文档:
PUT test/_doc/1
{
"counter" : 1,
"tags" : "red"
}
现在我想更新或插入文档,无论它是否已经存在:
POST test/_update/2
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}
在我的情况下,_doc=2
不存在,为此我添加upsert
到查询中,以便在 _doc 不存在时自动创建它。
相反,我收到此错误消息:
{
"error": {
"root_cause": [
{
"type": "invalid_type_name_exception",
"reason": "Document mapping type name can't start with '_', found: [_update]"
}
],
"type": "invalid_type_name_exception",
"reason": "Document mapping type name can't start with '_', found: [_update]"
},
"status": 400
}
请问我是不是误解了它的工作原理?
更新
映射:
PUT /test
{
"mappings": {
"type_name": {
"properties": {
"counter" : { "type" : "integer" },
"tags": { "type" : "text" }
}}},
"settings": {
"number_of_shards": 1
}
}
弹性搜索版本:“6.8.4”
解决方案
试试这个
您正在查看 7.x 文档,这是您的版本的文档:https ://www.elastic.co/guide/en/elasticsearch/reference/6.8/docs-update.html
POST test/type_name/2/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"counter" : 1
}
}
推荐阅读
- macos - ForFiles MacOS 替代品
- python - 如何返回特定目录?
- colors - Progress-4GL 有颜色常数吗?
- mysql - MySQL 按日期分组用户通知并组合内容
- python-3.x - 使用 matplotlib 时数组的索引过多
- .net - 服务器如何强制浏览器使用 IPv4 或 IPv6 发送请求?服务器如何提取我的 IPv4 和 IPv6?
- ldap - Apache2 通过 IP 和 ldap 限制访问
- react-native - 对象属性更改不会导致 React-Native 中的重新渲染
- node.js - GET URL 给了我一个 net::ERR_SSL_PROTOCOL_ERROR,我如何在我的控制台中摆脱这个错误?
- flutter - 适合文本到容器的 FittedBox 不起作用