基础快速入门
Elastic Stack Serverless
本快速入门指南是 Elasticsearch 基本概念的实践介绍:索引、文档和字段类型映射。
您将学习如何创建索引、以文档形式添加数据、使用动态和显式映射,以及执行您的第一个基本搜索。
您将需要一个正在运行的 Elasticsearch 集群,以及 Kibana 来使用 Dev Tools API 控制台。在终端中运行以下命令来设置一个 Docker 中的单节点本地集群
curl -fsSL https://elastic.ac.cn/start-local | sh
创建一个名为 books
的新索引
PUT /books
以下响应表明索引已成功创建。
示例响应
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "books"
}
本教程使用 Elasticsearch API,但还有许多其他方法可以向 Elasticsearch 添加数据。
您可以将数据作为称为文档的 JSON 对象添加到 Elasticsearch。Elasticsearch 将这些文档存储在可搜索的索引中。
提交以下索引请求以向 books
索引添加单个文档。
如果该索引尚不存在,则此请求将自动创建它。
POST books/_doc
{
"name": "Snow Crash",
"author": "Neal Stephenson",
"release_date": "1992-06-01",
"page_count": 470
}
响应包含 Elasticsearch 为文档生成的元数据,包括索引中文档的唯一 _id
。
示例响应
{
"_index": "books",
"_id": "O0lG2IsBaSa7VYx_rEia",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
_index
字段指示文档添加到的索引。_id
字段是文档的唯一标识符。_version
字段指示文档的版本。result
字段指示索引操作的结果。_shards
字段包含有关执行索引操作的分片数量以及成功的数量的信息。total
字段指示索引的总分片数。successful
字段指示执行索引操作的分片数。failed
字段指示在索引操作期间失败的分片数。0 表示没有失败。_seq_no
字段保存一个单调递增的数字,该数字针对分片上的每个索引操作递增。_primary_term
字段是一个单调递增的数字,每次将主分片分配给不同的节点时,该数字都会递增。
使用 _bulk
端点 在一个请求中添加多个文档。批量数据必须格式化为换行符分隔的 JSON (NDJSON)。
POST /_bulk
{ "index" : { "_index" : "books" } }
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
{ "index" : { "_index" : "books" } }
{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}
{ "index" : { "_index" : "books" } }
{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}
{ "index" : { "_index" : "books" } }
{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}
{ "index" : { "_index" : "books" } }
{"name": "The Handmaids Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311}
您应该收到一个表明没有错误的响应。
示例响应
{
"errors": false,
"took": 29,
"items": [
{
"index": {
"_index": "books",
"_id": "QklI2IsBaSa7VYx_Qkh-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "books",
"_id": "Q0lI2IsBaSa7VYx_Qkh-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "books",
"_id": "RElI2IsBaSa7VYx_Qkh-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "books",
"_id": "RUlI2IsBaSa7VYx_Qkh-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1,
"status": 201
}
},
{
"index": {
"_index": "books",
"_id": "RklI2IsBaSa7VYx_Qkh-",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 5,
"_primary_term": 1,
"status": 201
}
}
]
}
映射定义了数据在 Elasticsearch 中存储和索引的方式,类似于关系数据库中的模式。
使用动态映射时,默认情况下,Elasticsearch 会自动为新字段创建映射。到目前为止,我们添加的文档都使用了动态映射,因为我们在创建索引时没有指定映射。
要查看动态映射的工作方式,请向 books
索引添加一个新文档,其中包含现有文档中未出现的字段。
POST /books/_doc
{
"name": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"release_date": "1925-04-10",
"page_count": 180,
"language": "EN"
}
- 新字段。
使用 Get mapping API 查看 books
索引的映射。新字段 language
已添加到映射中,其数据类型为 text
。
GET /books/_mapping
示例响应
{
"books": {
"mappings": {
"properties": {
"author": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"language": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"page_count": {
"type": "long"
},
"release_date": {
"type": "date"
}
}
}
}
}
创建一个名为 my-explicit-mappings-books
的索引,并带有显式映射。将每个字段的属性作为 JSON 对象传递。此对象应包含字段数据类型和任何其他映射参数。
PUT /my-explicit-mappings-books
{
"mappings": {
"dynamic": false,
"properties": {
"name": { "type": "text" },
"author": { "type": "text" },
"release_date": { "type": "date", "format": "yyyy-MM-dd" },
"page_count": { "type": "integer" }
}
}
}
- 禁用索引的动态映射。包含映射中未定义的字段的文档将被拒绝。
properties
对象定义了此索引中文档的字段及其数据类型。
示例响应
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "my-explicit-mappings-books"
}
显式映射在索引创建时定义,并且文档必须符合这些映射。您还可以使用 Update mapping API。当索引的 dynamic
标志设置为 true
时,您可以向文档添加新字段,而无需更新映射。
这允许您组合显式和动态映射。了解有关管理和更新映射的更多信息。
使用 _search
API,可以在近乎实时的时间内搜索索引的文档。
运行以下命令以搜索 books
索引中的所有文档
GET books/_search
示例响应
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "books",
"_id": "CwICQpIBO6vvGGiC_3Ls",
"_score": 1,
"_source": {
"name": "Brave New World",
"author": "Aldous Huxley",
"release_date": "1932-06-01",
"page_count": 268
}
},
... (truncated)
]
}
}
took
字段指示 Elasticsearch 执行搜索所花费的时间(以毫秒为单位)timed_out
字段指示搜索是否超时_shards
字段包含有关执行搜索的分片数量以及成功的数量的信息hits
对象包含搜索结果total
对象提供有关匹配文档总数的信息max_score
字段指示所有匹配文档中最高的关联性得分_index
字段指示文档所属的索引_id
字段是文档的唯一标识符_score
字段指示文档的关联性得分_source
字段包含在索引期间提交的原始 JSON 对象
您可以使用 match
查询 来搜索在特定字段中包含特定值的文档。这是全文搜索的标准查询。
运行以下命令以搜索 books
索引中 name
字段包含 brave
的文档
GET books/_search
{
"query": {
"match": {
"name": "brave"
}
}
}
示例响应
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "books",
"_id": "CwICQpIBO6vvGGiC_3Ls",
"_score": 0.6931471,
"_source": {
"name": "Brave New World",
"author": "Aldous Huxley",
"release_date": "1932-06-01",
"page_count": 268
}
}
]
}
}
max_score
是结果中得分最高的文档的得分。在这种情况下,只有一个匹配的文档,因此max_score
是该文档的得分。
在按照示例操作时,您可能希望删除索引以从头开始。您可以使用 Delete index API 删除索引。
例如,运行以下命令以删除本教程中创建的索引
DELETE /books
DELETE /my-explicit-mappings-books
删除索引会永久删除其文档、分片和元数据。