Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert multiple records in one query?

for example :

curl -XPUT 'http://localhost:9200/test/users/2' -d '{ "firstname"   : "test" }'

insert only one record.

How to insert multiple records in one query?

like image 805
Bdfy Avatar asked Jul 16 '14 10:07

Bdfy


4 Answers

You need to use elasticsearch Bulk API. It allows you to insert multiple items with one request. Requests are POSTed to special endpoint /_bulk and look like this:

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "field1" : "value2" }
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
like image 76
Konstantin V. Salikhov Avatar answered Oct 30 '22 13:10

Konstantin V. Salikhov


I don't have enough karma to comment in response to @Tadzys, but yes, bulk inserting documents to one or more indices without an id seems doable. See "Inserting documents belonging to different types and indices in the same request" in Elasticsearch: Bulk Inserting Examples.

From the referenced page:

POST http://path.to.your.cluster/_bulk
{ "index":{ "_index": "myIndex", "_type": "person" } }
{ "name":"john doe","age":25 }
{ "index":{ "_index": "myOtherIndex", "_type": "dog" } }
{ "name":"fido","breed":"chihuahua" }

"Examples work for Elasticsearch versions 1.x, 2.x and probably later ones too."

like image 20
Nels Avatar answered Oct 30 '22 12:10

Nels


Better late than never. This is how i made a curl request to the elastic server to dump multiple records using the Bulk API

curl -XPOST 'http://<my_host_name>:9201/_bulk?pretty' -H 'Content-Type: application/json' -d '{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "1"} }{"id" : "AAA" , "name" : "AAAAAAAAAAAAAA"}{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "2"} }{"id" : "BBB" , "name" : "BBBBBBBBBBBBBBB"}{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "3"} }{"id" : "CCC" , "name" : "CCCCCCCCCCCCCCCC"}{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "4"} }{"id" : "DDD" , "name" : "DDDDDDDDDDDDDDDDDD"}{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "5"} }{"id" : "EEE" , "name" : "EEEEEEEEEEEEEEEEEE"}'

P.S: Elastic 5.x(atleast) doesn't seem to allow bulk create without an id attribute. Had to painfully come up with one for 100's of such records

like image 28
Hari K Murthy Avatar answered Oct 30 '22 12:10

Hari K Murthy


All answers above are right. I have a more specific one for you.

the port is 9200, not 9201 as @Hari K Murthy's answer. Or maybe there're some other configurations.

The post url address is: http://127.0.0.1:9200/_bulk?pretty. You may want to change to your own ip address.

Posted json is:

{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "1"} }
{"id" : "AAA" , "name" : "AAAAAAAAAAAAAA"}
{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "2"} }
{"id" : "BBB" , "name" : "BBBBBBBBBBBBBBB"}

eg: use bulk api in postman

NOTE: you have to format your json like one line metadata, another line data as the bulk api doc says.

like image 45
Bruce Lee Avatar answered Oct 30 '22 13:10

Bruce Lee