How to insert data into elasticsearch

CurlElasticsearch

Curl Problem Overview


I am new to Elasticearch, and I have been trying for 2 days to insert some data into Elasticearch. I found on Google that there are many pages to help to create an index (I am not clear about "index", does it mean "insert" in other terms?) Then many places give some curl command, and I really don't know where to execute these code lines to insert data. Example:

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

I am using Window 7 and I have installed Java and run elasticsearch successfully. Could anybody show me more details about how to insert data into Elasticearch

Many thanks

Curl Solutions


Solution 1 - Curl

You have to install the curl binary in your PC first. You can download it from here.

After that unzip it into a folder. Lets say C:\curl. In that folder you'll find curl.exe file with several .dll files.

Now open a command prompt by typing cmd from the start menu. And type cd c:\curl on there and it will take you to the curl folder. Now execute the curl command that you have.

One thing, windows doesn't support single quote around around the fields. So you have to use double quotes. For example I have converted your curl command like appropriate one.

curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/indexname/typename/optionalUniqueId" -d "{ \"field\" : \"value\"}"

Solution 2 - Curl

Let me explain clearly.. If you are familiar With rdbms.. Index is database.. And index type is table.. It mean index is collection of index types., like collection of tables as database (DB).

in NOSQL.. Index is database and index type is collections. Group of collection as database..

To execute those queries... U need to install CURL for Windows.

Curl is nothing but a command line rest tool.. If you want a graphical tool.. Try

Sense plugin for chrome...

Hope it helps..

Solution 3 - Curl

If you are using KIBANA with elasticsearch then you can use below RESt request to create and put in the index.

CREATING INDEX:

http://localhost:9200/company
PUT company
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "analysis": {
      "analyzer": {
        "analyzer-name": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      }
    }
  },
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "analyzer": "analyzer-name"
        }
      }
    }
  }
}

CREATING DOCUMENT:

POST http://localhost:9200/company/employee/2/_create
{
"name": "Hemani",
"age" : 23,
"experienceInYears" : 2
}

Solution 4 - Curl

To test and try curl requests from Windows, you can make use of Postman client Chrome extension. It is very simple to use and quite powerful.

Or as suggested you can install the cURL util.

A sample curl request is as follows.

curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"user" : "Arun Thundyill Saseendran",
"post_date" : "2009-03-23T12:30:00",
"message" : "trying out Elasticsearch"
}' "http://10.103.102.56:9200/sampleindex/sampletype/"

I am also getting started with and exploring ES in vast. So please let me know if you have any other doubts.

EDIT: Updated the index name and type name to be fully lowercase to avoid errors and follow convention.

Solution 5 - Curl

I started off using curl, but since have migrated to use kibana. Here is some more information on the ELK stack from elastic.co (E elastic search, K kibana): https://www.elastic.co/elk-stack

With kibana your POST requests are a bit more simple:

POST /<INDEX_NAME>/<TYPE_NAME>
{
    "field": "value",
    "id": 1,
    "account_id": 213,
    "name": "kimchy"
}

Solution 6 - Curl

To avoid using curl or Chrome plugins you can just use the the built in windows Powershell. From the Powershell command window run

Invoke-WebRequest -UseBasicParsing "http://127.0.0.1:9200/sampleindex/sampleType/" -
Method POST -ContentType "application/json" -Body '{
"user" : "Test",
"post_date" : "2017/11/13 11:07:00",
"message" : "trying out Elasticsearch"
}'

Note the Index name MUST be in lowercase.

Solution 7 - Curl

if someone wants just to copy-paste the command to send data to elastic search with username and password using curl:

make sure you change the command:

  • ELASTIC SEARCH URL
  • USERNAME
  • PASSWORD

here is the command:

curl -u YOUR_USERNAME:YOUR_PASSWORD -H "Content-Type: application/json" -XPOST "https://YOUR-ELASTICSEARCH-URL.com/adam/test" -d '{ "hello" : "world"}'

that's it.

Please note that:

  • the path of the command size is 2 (adam/test) - then your can post to elastic search.

  • change the message to whatever you want e.g. '{ "data" : "good"}'

  • you don't have to use username and password - just remove this from the command:
    -u YOUR_USERNAME:YOUR_PASSWORD

  • if you want to see data in kibana, don't forget to add index in kibana. (just in the UI menu - search for index pattern and add star *)

  • a successful message looks something like this: {"_index":"hello","_type":"world","_id":"QyBtfXsB-dsakdew29","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}%

Solution 8 - Curl

Simple fundamentals, Elastic community has exposed indexing, searching, deleting operation as rest web service. You can interact elastic using curl or sense(chrome plugin) or any rest client like postman.

If you are just testing few commands, I would recommend can use of sense chrome plugin which has simple UI and pretty mature plugin now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1162069View Question on Stackoverflow
Solution 1 - CurlSabuj HassanView Answer on Stackoverflow
Solution 2 - CurlBlackPOPView Answer on Stackoverflow
Solution 3 - CurlKailash KarkiView Answer on Stackoverflow
Solution 4 - CurlArun Thundyill SaseendranView Answer on Stackoverflow
Solution 5 - CurlDavid John Coleman IIView Answer on Stackoverflow
Solution 6 - CurlrobView Answer on Stackoverflow
Solution 7 - CurlNimitackView Answer on Stackoverflow
Solution 8 - CurlNitinView Answer on Stackoverflow