Friday, April 13, 2012

Inserting data into elasticsearch over HTTP: a breakdown

There are a zillion examples of what to type to insert into elasticsearch. But what does each part mean?

Shoving information into elasticsearch is pretty easy. You don't have to set anything up. (so, if you have a typo, good luck figuring out later where your data went. This is the price of sensible defaults.)

Here is one way to throw some data into an index - type this at a *nix prompt:

curl -XPOST "http://localhost:9200/indexname/typename/optionalUniqueId" -d '{ "field" : "value" }'

Here is what that means:
curl is a command that sends messages over HTTP.
-X is the option that tells curl which HTTP command to send. GET is the default.
POST is one of the HTTP commands that you can use for this insertion. PUT will work as well, but then the optionalUniqueId is not optional.
localhost is the machine where elasticsearch is running.
9200 is the default port number for elasticsearch
indexname is the name of your index. This must be in all lowercase. You can use different indexes to restrict your searches later. Also, indexes are associated with particular mapping configurations. The defaults are sensible, but know that you can configure stuff by index and search by index (or multiple indexes).
typename describes the type of document you're sticking into the index. You can use this later to narrow searches. Also, the ID of each document in the index should be unique per type.
optionalUniqueId if you have an intelligent ID for the document you're sticking in, then put it here. Otherwise elasticsearch will create one. When you want to update your object, you'll need this. it's also handy for retrieval of exactly one object.
-d tells curl "here comes the data!"
{ "field" : "value" } represents any valid JSON. all this stuff is stored for your object.

The output of this is an HTTP 200 if the document was updated or HTTP 201 if a document was created. If you want curl to tell you what http status code came back, add this to your command line: -w " status: %{http_code}"

Here are two of the easiest ways to see what you just inserted.

Retrieve by ID:

curl "http://localhost:9200/indexname/typename/optionalUniqueId?pretty=true"

This does a GET to fetch the object by ID.
?pretty=true tells elasticsearch to put newlines and indentation into the JSON so that it's easier for humans to read.

Retrieve everything in the index:
curl "http://localhost:9200/indexname/_search?pretty=true"

_searchtells elasticsearch that this is a query. Since no parameters are provided, everything is returned.
Notice that typename is omitted here. If you include it, then you'll get back everything of that type in the index.


  1. Thanks for taking the time to put this together. It provided me a very good starting point for feeding data into elasticsearch.

  2. Its very helpful for beginners. Thanks a ton

  3. curl -XGET "http://localhost:9200/indexname/_search?pretty=true"

    Make sure that you put -XGET for Retrieve by ID and Retrieve everything in the index.. then works perfectly

  4. Sorry, I am newbies to ElasticSearch, may I ask that where can I run the curl-XGet .... I google many place I still don't see any indicate how to run it?

  5. This comment has been removed by the author.

  6. @Phan: you must run it in the shell (linux or OSX 'bash', or in Windows Command Prompt 'cmd.exe')

  7. Analyze cricket is now dull and also prosaic. Some concrete floor proposals to be able to revamp the particular test cricket are already suggested simply by former Native indian cricket person Ravi Sasthri.

  8. Grass bigger picture tiles provide your playground a distinctive and appealing look. These tiles are made and produced from the motivation of organic grass. We possess tried to create a unique as well as eye getting product that provides the seem like the organic grass. The color is therefore cool and provide a grassy contact. GRASS PUZZLE TILES

  9. Dining car soon came to Birkenstock sandals clearance beside stewardess leaned prefer to give her knife and fork place,
    Mens Birkenstock Sandals see the table filled with documents, flight attendants made difficult.
    Key data is outlined Womens Birkenstock Sandals had to pen down, lift eye stewardess said:. "I just need a cup of coffee, please."
    Birkenstock sandals clearance Smiling stewardess nodded, handed her hand down on the coffee.
    Mens Birkenstock Sandals is preparing to take over, at this moment, the cabin suddenly burst bumps.
    Womens Birkenstock Sandals hastened to reach out and care, into a brief scene of chaos, partially pulled out in an emergency
    KD 9 Elite finally survived to the file, but she already is a large stain on the skirt.
    Then came the captain's announcement also notice: KD 8 Elite to fly a short stream, passengers please fasten your seat belts.
    KD Elite,whatever the outcome is the fastest drying coffee file, seeing a few files falling into the aisle behind,
    KD 9 Elite attend himself ticking under coffee, the solution of the seat belt, got to pick up.
    KD Elite picked up her first step file -
    Cheap under armour shoes is sitting in the back seat of a passenger ramp.
    Under Armour Curry 1 is the first to see the other side from the roll to elbow exposed parts of the shirt lean arm,
    Mens under armour shoes wearing watches, slender fingers.
    Cheap under armour shoes reached out, took the papers say thank you, but before the opening stunned.