elasticsearch 无法在Elasticsearch中创建映射和添加数据
每次我按照有关在elasticsearch中创建索引、映射和添加数据的说明操作时,我都会出错。 我用的是邮递员。 首先,我创建索引:
elasticsearch 无法在Elasticsearch中创建映射和添加数据,
elasticsearch,
elasticsearch,每次我按照有关在elasticsearch中创建索引、映射和添加数据的说明操作时,我都会出错。 我用的是邮递员。 首先,我创建索引: POST http://localhost:9200/schools (实际上,我必须使用put成功创建) 接下来,我创建映射并添加数据: POST http://localhost:9200/schools/_bulk 请求主体 { "index":{ "_index":"schools", "_type":"sch
POST http://localhost:9200/schools
(实际上,我必须使用put成功创建)
接下来,我创建映射并添加数据:
POST http://localhost:9200/schools/_bulk
请求主体
{
"index":{
"_index":"schools", "_type":"school", "_id":"1"
}
}
{
"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
"city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
}
{
"index":{
"_index":"schools", "_type":"school", "_id":"2"
}
}
{
"name":"Saint Paul School", "description":"ICSE
Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075",
"location":[28.5733056, 77.0122136], "fees":5000,
"tags":["Good Faculty", "Great Sports"], "rating":"4.5"
}
{
"index":{"_index":"schools", "_type":"school", "_id":"3"}
}
{
"name":"Crescent School", "description":"State Board Affiliation", "street":"Tonk Road",
"city":"Jaipur", "state":"RJ", "zip":"176114","location":[26.8535922, 75.7923988],
"fees":2500, "tags":["Well equipped labs"], "rating":"4.5"
}
但我得到的只是:
{
"error": {
"root_cause": [
{
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@681c6189; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@681c6189; line: 2, column: 3]"
}
],
"type": "json_e_o_f_exception",
"reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@681c6189; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@681c6189; line: 2, column: 3]"
},
"status": 500
}
这是因为您的请求主体JSON格式不正确。我建议只使用一个条目进行检查,直到您可以将其输入Elasticsearch,然后添加其他条目 以下JSON是有效的,但我不确定它是否提供了您想要的结构:
{
"index":{
"_index":"schools", "_type":"school", "_id":"1"
},
"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan",
"city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],
"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"
}
您可以使用一个格式化和验证JSON的工具来确保它是有效的JSON。下面是一些例子
我看到了与我的问题类似的东西。我的问题解决了
要将数据加载到Elasticsearch,请使用REST API端点“/\u bulk”,该端点需要 以下换行符分隔的JSON(NDJSON)结构:
action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n
卷曲请求示例:
curl-H'Content Type:application/x-ndjson'-XPOST'elasticsearchhost:port/index name sample/_bulk?pretty'-data binary@sample.json
在您的情况下,请求如下:
curl-H'内容类型:application/x-ndjson'-XPOST'localhost:9200/schools/_bulk?pretty'-data binary@schools-sample.json
schools-sample.json内容:
{"index":{"_index":"schools", "_type":"school", "_id":"1"}}
{"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan","city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385, 76.8380405],"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"}
{"index":{"_index":"schools", "_type":"school", "_id":"2"}}
{"name":"Saint Paul School", "description":"ICSE Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075","location":[28.5733056, 77.0122136], "fees":5000,"tags":["Good Faculty", "Great Sports"], "rating":"4.5"}
/n
重要信息:数据的最后一行必须以换行符结尾\n。每个换行符前面都可以有回车符\r\n。否则,您将得到一个错误:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
}
],
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
},
"status" : 400
}
这是错误的。大容量插入api不接受格式正确的json对象,而是一个以行分隔的json对象列表。很抱歉它应该是:{code>{index:{u index:{u index:“学校”,“学校类型”,“学校id:“1”}{“名称”:“中心学校”,“描述”:“CBSE附属机构”,“街道”:“纳甘”,“城市”:“纸屑”,“州”:“HP”,“zip:“176115”,“位置”:[31.8955385,76.8380405],“费用”:2000,“标签”:[“高中”,“美丽校园],“评级”:“3.5\n”}使用换行符分隔要发布到ES的每个文档。如果您想先检查这是否有效,那么我将编辑我的答案以反映这一点。很抱歉,评论中的格式不是很好。
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
}
],
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
},
"status" : 400
}