trungducng
trungducng

Reputation: 397

Cannot Create Mapping and Add data in Elasticsearch

Everytime I follow the instruction about Create Index, Mapping and Add Data in elasticsearch i have the error. I'm using Postman. First of all, i create index:

POST http://localhost:9200/schools

(actually, i have to use put to create succesfully)

Next, i create Mapping and Add Data:

POST http://localhost:9200/schools/_bulk

Request Body

    {
       "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"
    }

But all i receive is just:

   {
      "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
    }

Upvotes: 5

Views: 5645

Answers (4)

Dmitry Kokora
Dmitry Kokora

Reputation: 837

To load data to Elasticsearch, use the REST API endpoint is '/_bulk' which expects the following newline delimited JSON (NDJSON) structure:

action_and_meta_data\n
optional_source\n
....
action_and_meta_data\n
optional_source\n

The Curl request example: curl -H 'Content-Type: application/x-ndjson' -XPOST 'elasticsearchhost:port/index-name-sample/_bulk?pretty' --data-binary @sample.json

In your case, the request will be as follows: curl -H 'Content-Type: application/x-ndjson' -XPOST 'localhost:9200/schools/_bulk?pretty' --data-binary @schools-sample.json

The schools-sample.json content:

{"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

Important: the final line of data must end with a newline character \n. Each newline character may be preceded by a carriage return \r. Otherwise, you will get an error:

{
  "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
}

Upvotes: 1

F Usman
F Usman

Reputation: 31

{ "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\n"}
{ "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" }
{ "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\n"}

Upvotes: 0

trungducng
trungducng

Reputation: 397

I see something which similar to my problem. My problem solved!

Elasticsearch Bulk API - Unexpected end-of-input: expected close marker for ARRAY

Upvotes: 1

Donglecow
Donglecow

Reputation: 497

This is because your request body JSON is malformed. I'd advise checking with just one entry until you can get it into Elasticsearch, then add the others.

The following JSON is valid, though I'm not sure if it provides the structure you want:

{
   "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"
}

You can use a tool for formatting and validating JSON to make sure it is valid JSON. Below are some examples.

http://jsonformatter.org/

https://jsonformatter.curiousconcept.com/

Upvotes: 7

Related Questions