ES添加数据

62 阅读1分钟

要向Elasticsearch索引添加数据,首先需要设置好索引的映射和字段类型。然后您可以使用Elasticsearch提供的API来添加数据。具体步骤如下:

1.创建一个索引

PUT /my_index
{
  "mappings": {
    "properties": {
      "title": {"type": "text"},
      "content": {"type": "text"}
    }
  }
}

2.使用Index或Bulk API添加数据

POST /my_index/_doc
{
  "title": "Example document",
  "content": "This is some sample content"
}

POST /my_index/_bulk
{ "index": { "_id": "1" }}
{ "title": "Sample document 1", "content": "Some sample content" }
{ "index": { "_id": "2" }}
{ "title": "Sample document 2", "content": "More sample content" }

这些操作将会向索引my_index中添加数据。请注意,您需要使用正确的端点(URL)和身份验证(如果需要)。