elasticsearch - 当文档不存在时,Upsert不会添加新的(ElasticSearch)

标签 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
  }
}

ElasticSearch版本:“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
    }
}

关于elasticsearch - 当文档不存在时,Upsert不会添加新的(ElasticSearch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58846206/

相关文章:

elasticsearch - Elasticsearch AND条件与词条查询

elasticsearch - Elasticsearch 使用范围查询在字段中找到差异

mongodb - 在Elasticsearch中尝试将MongoDB文档作为单独的类型导入

elasticsearch - Elasticsearch词查询不匹配特殊字符

elasticsearch - 匹配查询的格式较短

java - elasticsearch中执行From/Size时的范围

node.js - 在Elasticsearch中是否有搜索精确词和包含词的解决方案

elasticsearch - 在ElasticSearch NEST客户端中建立索引时设置一致性级别

c# - 使用 Nest 进行 Elasticsearch

elasticsearch - 在Elasticsearch索引中存储MD5哈希的正确方法