使用Elasticsearch API索引和搜索数据
edit使用Elasticsearch API索引和搜索数据
edit本快速入门指南是关于Elasticsearch基本概念的实践介绍:索引、文档和字段类型映射。
您将学习如何创建索引、将数据作为文档添加、处理动态和显式映射,以及执行您的第一个基本搜索。
先决条件
edit在开始之前,您需要有一个正在运行的 Elasticsearch 集群。 最快入门的方法是使用 本地开发环境。 请参阅 运行 Elasticsearch 以获取其他部署选项。
步骤1:创建索引
edit创建一个名为 books
的新索引:
PUT /books
以下响应表示索引已成功创建。
示例响应
{ "acknowledged": true, "shards_acknowledged": true, "index": "books" }
步骤 2:向索引中添加数据
edit本教程使用Elasticsearch API,但还有许多其他方法可以将数据添加到Elasticsearch。
您将数据作为称为文档的JSON对象添加到Elasticsearch中。 Elasticsearch将这些文档存储在可搜索的索引中。
添加单个文档
edit提交以下索引请求以将单个文档添加到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 }
The |
|
字段 |
|
The |
|
字段 |
|
The |
|
字段 |
|
The |
|
The |
|
The |
|
The |
添加多个文档
edit使用_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 } } ] }
步骤 3:定义映射和数据类型
edit映射定义了数据在Elasticsearch中如何存储和索引,类似于关系数据库中的模式。
使用动态映射
edit当使用动态映射时,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
索引的映射。新字段new_field
已添加到映射中,数据类型为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 } } }, "new_field": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "page_count": { "type": "long" }, "release_date": { "type": "date" } } } } }
定义显式映射
edit创建一个名为 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" } } } }
示例响应
{ "acknowledged": true, "shards_acknowledged": true, "index": "my-explicit-mappings-books" }
结合动态和显式映射
edit显式映射在索引创建时定义,文档必须符合这些映射。
你也可以使用更新映射 API。
当索引的dynamic
标志设置为true
时,你可以在不更新映射的情况下向文档添加新字段。
这使您能够结合显式和动态映射。 了解更多关于管理和更新映射的信息。
步骤 4:搜索您的索引
edit索引文档几乎可以实时用于搜索,使用_search
API。
搜索所有文档
edit运行以下命令以搜索 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) ] } }
The |
|
The |
|
The |
|
The |
|
The |
|
The |
|
The |
|
The |
|
The |
|
The |
匹配
查询
edit您可以使用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 } } ] } }
步骤 5:删除您的索引(可选)
edit当跟随示例操作时,您可能希望删除索引以从头开始。 您可以使用删除索引 API来删除索引。
例如,运行以下命令以删除在本教程中创建的索引:
DELETE /books DELETE /my-explicit-mappings-books
删除索引将永久删除其文档、分片和元数据。