首页 > 解决方案 > 我们如何在 Elastic Search 中插入文档?‽‽?

问题描述

我正在关注教程: https ://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-index.html

当我复制插入文档的命令,然后将其粘贴到 windows cmd 中时,我们会看到以下内容:

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}
curl: (6) Could not resolve host: application

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>{
"{" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>  "name": "John Doe"
""name":" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>}
"}" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>'
"'" no se reconoce como un comando interno o externo,
programa o archivo por lotes ejecutable.

因此,我编辑了命令以将其放入一行中:

curl -XPUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d '{"name":"John Doe"}'

当我们执行它时, cmd 输出:

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>curl -XPUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d '{"name":"John Doe"}'
{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}
curl: (6) Could not resolve host: application

我读:

Elasticsearch 不支持 Content-Type 标头 [application/x-www-form-urlencoded]

我检查了我是否已经写过:-H 'Content-Type: application/json'

除了我读到:

不支持 Content-Type 标头 [application/x-www-form-urlencoded]

我试图在 localhost 和 content-type 周围加上双引号:

curl -XPUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type: application/json" -d '{"name":"John Doe"}'

执行后我们观察到:

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>curl -XPUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type: application/json" -d '{"name":"John Doe"}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "not_x_content_exception",
        "reason" : "not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse",
    "caused_by" : {
      "type" : "not_x_content_exception",
      "reason" : "not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
    }
  },
  "status" : 400
}

另外我尝试用单引号替换两个引号,然后在这里我们有我们的 cmd 跟踪:

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>curl -XPUT 'localhost:9200/customer/_doc/1?pretty' -H 'Content-Type: application/json' -d '{"name":"John Doe"}'
curl: (6) Could not resolve host: 'localhost
curl: (6) Could not resolve host: application

另外要检查我们是否已启动并运行 Elastic Search:

C:\Users\ymorenoj\Downloads\elasticsearch-7.5.2-windows-x86_64\elasticsearch-7.5.2\bin>curl -X GET "localhost:9200/_cat/health?v&pretty"
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1580922144 17:02:24  elasticsearch green           2         2      2   1    0    0        0             0                  -                100.0%

怎么解决?‽‽?

标签: stringelasticsearchcmd

解决方案


哈哈,windows 和 curl 可能很麻烦...

在 Windows 中,此版本有效:

curl -XPUT "localhost:9200/customer/_doc/1?pretty" -H "Content-Type: application/json" -d "{""name"":""John Doe""}"

请注意 json 中的双引号。

如果您在 Windows 下工作,请尝试使用 Windows Linux 子系统 (WSL) 或 Cygwin。这会对你有很大帮助。另一种选择是安装 kibana 并使用开发工具


推荐阅读