91

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

Nadjib Mami
  • 5,736
  • 9
  • 37
  • 49
  • The elastic.co website now carries a page to address this OP's question. Check it out at https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-index.html. – Sonny Apr 05 '21 at 14:28

8 Answers8

100

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\"}"
tedder42
  • 23,519
  • 13
  • 86
  • 102
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 1
    Thank you Sabuj, I have instal CURL and run the code, it create in 'data' folder a indexname. But anytime I need to insert data to ElasticSearch, I must use these tool like CURL, RESTClient... As my understand, ElasticSearch use webservice to contact with data, am I right? In the case I need to interact with Elastic using my application, how can I do it? Many thanks –  Apr 06 '14 at 03:19
  • I do not have much idea about `ElasticSearch`. May be doing `POST` `GET` request from your application will do. Not sure cause I don't know how to make an application using `ElasticSearch`. – Sabuj Hassan Apr 06 '14 at 07:24
  • 1
    Thank you Sabuj Hassan, I will come back when i found something more. But could you please also help to insert a .json file into Elastic? I can now insert a single test record in Elastic, but I have about 200 .json file need to put into it. The code line curl -XPOST "http://localhost:9200/pattentdb/pattent/1" -@data1.json not work for me (data1.json is my .json file) –  Apr 06 '14 at 10:22
  • Thank you Sabuj Hassan, you help me so much. By the way, I have to use the fullpath of the file instead for using only the filename with extension(I just think that it will recognise the file in the same directory, but it does not) –  Apr 06 '14 at 10:53
  • @tedder42 can you explain why we use \" on windows? why only within {}? – chaikov Oct 23 '19 at 02:22
18

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
}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Kailash Karki
  • 2,106
  • 1
  • 12
  • 6
16

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..

Pooja
  • 805
  • 1
  • 7
  • 16
BlackPOP
  • 5,657
  • 2
  • 33
  • 49
  • Dear BlackPop, thank you for your reply. I have read and now using RESTClient. I try to search for Sense plugin for chrome, but there is only AdSense in Google appstore. By the way, with these tool, how can I insert a record into Elatic? Many thanks –  Apr 06 '14 at 03:13
  • Sense plug in a made for elasticsearch. Try sense plug in for chrome.. It's available for chrome browser alone.. Try it – BlackPOP Apr 06 '14 at 06:36
  • 1
    Index has a lot of different meanings in Elasticsearch. It is also an **action**. If you index a document, you are adding it to Elasticsearch for indexing. Keep in mind, Elasticsearch is a search engine for the data you are storing in it. – Ellesedil Oct 14 '14 at 14:18
  • just to add, "type" can be compared to tables in relational DB but is actually non existent in elasticsearch or more specifically Lucene library,it seems in 7 version type to be removed and the index would have firleds directly to align with lucene. – Ashish Dadhore Apr 17 '18 at 14:24
9

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.

  • From that code I get back an error that the index name must be lowercase. Changed to simpleindex and call succeeded. – rob Nov 13 '17 at 11:03
4

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

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.

rob
  • 8,134
  • 8
  • 58
  • 68
0

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}%

Adam Delarosa
  • 901
  • 1
  • 10
  • 20
-6

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.

Nitin
  • 21
  • 2