proger2014
proger2014

Reputation: 311

how insert data to Elasticsearch without id

I insert data to Elasticsearch with id 123

localhost:9200/index/type/123

but I do not know what will next id inserted

how insert data to Elasticsearch without id in localhost:9200/index/type?

Upvotes: 11

Views: 20304

Answers (6)

Juan Carlos Gallegos
Juan Carlos Gallegos

Reputation: 59

In my case, I was trying to add a document directly to an index, e.g. localhost:9200/messages, as opposed to localhost:9200/someIndex/messages.

I had to append /_doc to the URL for my POST to succeed: localhost:9200/messages/_doc. Otherwise, I was getting an HTTP 405:

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

Here's my full cURL request:

$ curl -X POST "localhost:9200/messages/_doc" -H 'Content-Type:
application/json' -d'
{
  "user": "Jimmy Doe",
  "text": "Actually, my only brother!",
  "timestamp": "something"
}
'

{"_index":"messages","_type":"_doc","_id":"AIRF8GYBjAnm5hquWm61","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":3}

Upvotes: 5

Raj Hirani
Raj Hirani

Reputation: 192

If our data doesn’t have a natural ID, we can let Elasticsearch autogenerate one for us. The structure of the request changes: instead of using the PUT verb ("store this document at this URL"), we use the POST verb ("store this document under this URL"). The URL now contains just the _index and the _type:

curl -X POST "localhost:9200/website/blog/" -H 'Content-Type: application/json' -d'
{
  "title": "My second blog entry",
  "text":  "Still trying this out...",
  "date":  "2014/01/01"
}
'

The response is similar to what we saw before, except that the _id field has been generated for us:

{
   "_index":    "website",
   "_type":     "blog",
   "_id":       "AVFgSgVHUP18jI2wRx0w",
   "_version":  1,
   "created":   true
}

Autogenerated IDs are 20 character long, URL-safe, Base64-encoded GUID strings. These GUIDs are generated from a modified FlakeID scheme which allows multiple nodes to be generating unique IDs in parallel with essentially zero chance of collision.

https://www.elastic.co/guide/en/elasticsearch/guide/current/index-doc.html

Upvotes: 2

German P.
German P.

Reputation: 69

In my case, using nodejs and the elasticsearch package I did it this way using the client:

client.index ()

var elasticsearch = require ('elasticsearch');
let client = new elasticsearch.Client ({
    host: '127.0.0.1: 9200'
});

client.index ({
    index: 'myindex'
    type: 'mytype',
    body: {
        properti1: 'val 1',
        properti2: ['y', 'z'],
        properti3: true,
    }
}, function (error, response) {
    if (error) {
        console.log("error: ", error);
    } else {
        console.log("response: ", response);
    }
});

if an id is not specified, elasticsearch will generate one automatically

Upvotes: 3

Srinivas Damam
Srinivas Damam

Reputation: 3045

You can use POST request to create a new document or data object without specifying id property in the path.

curl -XPOST 'http://localhost:9200/stackoverflow/question' -d '
{
  title: "How to insert data to elasticsearch without id in the path?"
}

Upvotes: 2

s_munsey
s_munsey

Reputation: 1

It's possible to leave the ID field blank and elasticsearch will assign it one. For example a _bulk insert will look like

{"create":{"_index":"products","_type":"product"}}\n
{JSON document 1}\n
{"create":{"_index":"products","_type":"product"}}\n
{JSON document 2}\n
{"create":{"_index":"products","_type":"product"}}\n
{JSON document 3}\n
...and so on

The IDs will look something like 'AUvGyJMOOA8IPUB04vbF'

Upvotes: -1

Siddardha Budige
Siddardha Budige

Reputation: 1015

The index operation can be executed without specifying the id. In such a case, an id will be generated automatically. In addition, the op_type will automatically be set to create. Here is an example (note the POST used instead of PUT):

$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

Upvotes: 14

Related Questions