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

时间:2019-11-13 21:54:07

标签: 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”

1 个答案:

答案 0 :(得分:0)

尝试

您正在查看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
    }
}