如何在弹性搜索中插入数据

我是弹性搜索的新手,两天来我一直在尝试向 Elasticearch 插入一些数据。 我在 Google 上发现有很多页面可以帮助创建索引(我不太清楚“ index”是什么意思,它在其他术语中的意思是“插入”吗?) 然后,许多地方给出了一些 curl 命令,我真的不知道在哪里执行这些代码行来插入数据。例如:

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

我正在使用 Windows7,并且我已经安装了 Java 并且成功地运行了 elasticsearch。 有人能告诉我更多关于如何将数据插入 Elasticearch 的细节吗

非常感谢

271294 次浏览

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

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

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.

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.

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.

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

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
}

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