乐闻世界logo
搜索文章和话题

How to insert data into elasticsearch

4个答案

1
2
3
4

In Elasticsearch, inserting data is typically done by submitting JSON documents to the selected index via HTTP PUT or POST requests. Here are several common methods for inserting data:

Using HTTP PUT to Insert a Single Document

If you already know the ID of the document you want to insert, you can directly insert using the PUT method. For example:

http
PUT /index_name/_doc/document_id { "field1": "value1", "field2": "value2", ... }

In this example, index_name is the name of the index where you want to insert the document, _doc is the document type (which has been deprecated since Elasticsearch 7.x), document_id is the unique identifier for this document, followed by the JSON document content.

Using HTTP POST to Insert a Single Document

If you don't care about the document ID, Elasticsearch will automatically generate one for you. You can use the POST method to do this:

http
POST /index_name/_doc { "field1": "value1", "field2": "value2", ... }

In this example, Elasticsearch will automatically generate the document ID and insert the provided data.

Bulk Inserting Documents

When inserting multiple documents, you can use Elasticsearch's bulk API (_bulk API) to improve efficiency. Here is an example:

http
POST /_bulk { "index" : { "_index" : "index_name", "_id" : "1" } } { "field1" : "value1" } { "index" : { "_index" : "index_name", "_id" : "2" } } { "field1" : "value2" } ...

The bulk API accepts a series of operations, each consisting of two lines: the first line specifies the operation and metadata (such as _index and _id), and the second line contains the actual document data.

Using Client Libraries

Besides directly using HTTP requests, many developers prefer to use client libraries to interact with Elasticsearch. For example, in JavaScript, using the official elasticsearch client library, you can insert data as follows:

javascript
const { Client } = require('@elastic/elasticsearch'); const client = new Client({ node: 'http://localhost:9200' }); client.index({ index: 'index_name', id: 'document_id', body: { field1: 'value1', field2: 'value2', ... } }, (err, result) => { if (err) console.log(err); console.log(result); });

In this example, we create an Elasticsearch client instance and use its index method to insert a document. You can specify the document ID or let Elasticsearch generate it automatically.

In summary, inserting data into Elasticsearch typically involves sending HTTP requests containing JSON documents to the appropriate index, whether for a single document or multiple documents. Client libraries can simplify this process and provide more convenient and robust programming interfaces.

2024年6月29日 12:07 回复

I began using curl, but later migrated to using Kibana. For additional details about the ELK stack from elastic.co (E Elasticsearch, K Kibana), visit: https://www.elastic.co/elk-stack. Using Kibana, your POST request becomes simpler:

json
POST /<INDEX_NAME>/<TYPE_NAME> { "field": "value", "id": 1, "account_id": 213, "name": "kimchy" }
2024年6月29日 12:07 回复

If you are using Kibana with Elasticsearch, you can use the following REST request to create and index data.

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

Create Document

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

2024年6月29日 12:07 回复

You must first install the curl binary on your PC. You can download it here:

After extracting it to a folder, such as C:\curl, you will find curl.exe along with multiple .dll files in that folder.

Open the command prompt by entering content from a .txt file. Then type cd c:\curl to navigate to the curl folder. Now execute the curl command you have.

One thing to note is that Windows does not support single quotes around fields. Therefore, you must use double quotes. For example, I have converted your curl command to the appropriate one:

bash
curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/indexname/typename/optionalUniqueId" -d "{ \"field\" : \"value\" }"
2024年6月29日 12:07 回复

你的答案