json ElasticSearch 发送数据时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35213472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:16:15  来源:igfitidea点击:

ElasticSearch error while sending data

jsonelasticsearch

提问by noobEngineer

I am trying to send a JSON to elasticSearch. I have tried using Postman and SOAPUI The data is

我正在尝试向 elasticSearch 发送 JSON。我曾尝试使用 Postman 和 SOAPUI 数据是

[{"column1": "abc", "column2": "def", "column3": "dghi", "column4": "jkl", "column5": "mno"}, {"column1": "pqr", "column2": "stu", "column3": "vwx", "column4": "", "column5": ""}]

I am getting the following error back

我收到以下错误回复

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse",
    "caused_by": {
      "type": "not_x_content_exception",
      "reason": "Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"
    }
  },
  "status": 400
}

But when I post a single JSON array I.e.

但是当我发布一个 JSON 数组即

{"column1": "abc", "column2": "def", "column3": "dghi", "column4": "jkl", "column5": "mno"}

Then it works fine. I am new to ElasticSearch so not sure what is going wrong.

然后它工作正常。我是 ElasticSearch 的新手,所以不确定出了什么问题。

采纳答案by hellol11

Try using curly brackets first, then naming the array. Try that and see if it works.

先尝试使用大括号,然后命名数组。试试看,看看它是否有效。

For example:

例如:

{root:[1, 2, 3, 4, 5]}

might work because it's contained inside an object.

可能会起作用,因为它包含在一个对象中。

Even better; while I was experimenting with JSON.stringify();, I found that it coverts arrays to JSON like so:

甚至更好;当我尝试使用 时JSON.stringify();,我发现它可以像这样将数组转换为 JSON:

{1, 2, 3, 4, 5}

回答by Brandon Kearby

I recently ran into this using curl and it was a simple typo. I was not using inline json, but from a file. I forgot to include the @ sign before the filename.

我最近使用 curl 遇到了这个问题,这是一个简单的错字。我没有使用内联 json,而是从文件中使用。我忘了在文件名前包含 @ 符号。

curl -XPUT -H'Content-Type:application/json' localhost:9200/twitter [email protected]

回答by noobEngineer

thanks @hellol11

谢谢@hello11

it worked when I changed to

当我改为

    {root :
[
    {"column1": "abc", "column2": "def", "column3": "dghi", "column4": "jkl", "column5": "mno"},
    {"column1": "pqr", "column2": "stu", "column3": "vwx", "column4": "", "column5": ""}

]}

回答by Jung

In Windows environment, I solved this problem.

在Windows环境下,我解决了这个问题。

-d plus double quotation(") 

and surround name with backslash plus double quotaton(\")

并用反斜杠加上双引号(\")包围名称

command>

命令>

curl (more-option) -d "{\"column1\": \"abc\", \"column2\": \"def\", \"column3\": \"dghi\", \"column4\": \"jkl\", \"column5\": \"mno\"}"

回答by Scott Nestor

I had this issue inside a Python script (using requests) to try and POST to ES. The resolution was simply to convert the json object to a string using json.dumps()

我在 Python 脚本(使用请求)中遇到了这个问题来尝试 POST 到 ES。解决方法只是将 json 对象转换为字符串,使用json.dumps()

Example (Python snipit):

示例(Python 代码):

import json
import requests

headers={'Content-Type': 'application/json'}
data={'hello':'barney'} 

response = requests.post('https://<my_es_domain>/<my_es_ix>/<my_doc_type>', data=json.dumps(data), headers=headers)

回答by raviabhiram

I was getting the same error while trying to post to elasticsearch but using node js. I used the node-fetch package to make a PUT request to elasticsearch. It was weird because using the exact same thing on postman I got no error but I was getting the error on node js. I was initially doing:

我在尝试发布到 elasticsearch 但使用 node js 时遇到了同样的错误。我使用 node-fetch 包向 elasticsearch 发出 PUT 请求。这很奇怪,因为在邮递员上使用完全相同的东西我没有出错,但是我在节点 js 上收到了错误。我最初是这样做的:

const current = {
    "id" : "123456789"
}
const options = {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: current
}

Turns out the error got fixed when I changed it to:

原来,当我将其更改为时,错误得到了修复:

body: JSON.stringify(current)

回答by balram pundir

In elastic search ,if you want to post bulk data then each list object must be in New line .so if you are using sense then make everything in New line or if using from code then add New line character \n...

在弹性搜索中,如果您想发布批量数据,那么每个列表对象都必须在新行中。因此,如果您使用的是意义,则将所有内容都放在新行中,或者如果使用 from 代码,则添加新行字符 \n...