ElasticSearch 7.x 中,插入数据报错

问题描述

我使用的版本是 7.0.0, 由于 7.x 取消了 Type, 因此我是这么创建索引的:

$ curl -X PUT 'localhost:9200/accounts' -d '
{
  "settings":{
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": { 
    "properties": {
      "user": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      },
      "desc": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_max_word"
      }
    }
  }
}'

没有设定 Type(如果我设定 Type 则会报错),返回信息是创建成功:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "accounts"
}

接下来,我开始插入数据:

$ curl -X PUT 'localhost:9200/accounts/1' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}' 

却提示我:

{
    "error": "Incorrect HTTP method for uri [/accounts/1] and method [PUT], allowed: [POST]",
    "status": 405
}

好吧,那我就换成 Post 方式咯:

$ curl -X POST 'localhost:9200/accounts/1' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}' 

但是这样却提示:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Rejecting mapping update to [accounts] as the final mapping would have more than 1 type: [_doc, 1]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [accounts] as the final mapping would have more than 1 type: [_doc, 1]"
    },
    "status": 400
}

我不知道怎么就『多于一种类型』了。。。

之后我继续尝试,在 es 中,插入数据又分为指定文档id插入自动产生文档id插入,上面的尝试中,我使用了 Put 请求和 Post 请求,做了 指定文档id插入 的尝试,都不成功,于是我又使用 Post 请求,看看能不能 自动产生文档id插入

请求:

$ curl -X POST 'localhost:9200/accounts' -d '
{
  "user": "张三",
  "title": "工程师",
  "desc": "数据库管理"
}' 

响应:

{
    "error": "Incorrect HTTP method for uri [/accounts] and method [POST], allowed: [DELETE, PUT, GET, HEAD]",
    "status": 405
}

我。。。
求教,我现在应该怎么办

阅读 20k
2 个回答

Elasticsearch 7.x

  • Specifying types in requests is deprecated. For instance, indexing a document no longer requires a document type. The new index APIs are PUT {index}/_doc/{id} in case of explicit ids and POST {index}/_doc for auto-generated ids.
  • The include_type_name parameter in the index creation, index template, and mapping APIs will default to false. Setting the parameter at all will result in a deprecation warning.
  • The default mapping type is removed.

摘自 removal-of-types

指定ID的API是PUT {index}/_doc/{id}, 自动生成ID的API是POST {index}/_doc。你的请求路径里少了/_doc

还是要看官方文档。
README

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进