ES 索引API
Elasticsearch Index APIs
Elasticsearch 提供了 Index API 来管理索引的所有方面,例如索引模板、映射、别名和设置等。在 Elasticsearch,Index API 执行索引级别的操作。它负责管理不同的索引、索引设置、索引模板、映射、文件格式和别名。
当请求具有特定映射的索引时,此API 有助于添加或更新 JSON相应索引中的文档。它允许对索引执行许多操作,例如-创建、删除、分析、刷新、刷新等等。
有一个我们使用某些 API 对索引执行的操作列表。我们将通过示例详细讨论它们中的每一个:
Create Index
Get Index
Delete Index
Open/Close Index
Index Exist
Index Aliases
Analyze
Index Template
Index Settings
Index Stats
Flush
Refresh
创建索引
该API用于创建索引。可以通过发送不带正文或具有适当映射、设置和别名的 PUT 请求来创建索引。每当用户将 JSON 对象传递给任何索引时,都会自动创建一个索引。
在下面的示例中,我们将创建一个名为 book 的索引。
例如
POST http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
使用设置-
我们还可以在创建索引时在查询中添加一些设置,例如分片或副本-
POST http://localhost/9200/book/
{
"settings" : {
"index" : {
"number_of_shards" : 4,
"number_of_replicas" : 3
}
}
}
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
或者使用映射-
POST http://localhost/9200/book/_doc/
{
"settings": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 3
}
},
"mappings": {
"type1": {
"_source": {"enabled": false},
"properties": {
"book_name": {"type": "string"},
"author_name": {"type": "string"},
"published_date": {"type": "date"},
"price": {"type": "float"},
"publisher": {"type": "string"}
}
}
}
}
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
注意: 请注意,这里我们使用 POST 方法创建带有映射的索引,而在早期的简单索引创建中,我们使用了 PUT 方法。
Get Index
Get API 用于提供有关您想要的特定索引的信息。它获取索引的信息。只需发送一个 GET 请求和索引名称即可调用此 API,该请求将返回有关该特定索引的所有信息。它通过 Get 请求返回元数据。看下面的例子
例如
GET http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
{
"took": 2084,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "dkqwVnMB1ug6i0GdSAsj",
"_score": 1,
"_source": {
"setting": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 3
}
},
"mapping": {
"type1": {
"_source": {
"enabled": false
},
"properties": {
"book_name": {
"type":"string"
},
"author_name": {
"type":"string"
},
"publish_date": {
"type":"date"
},
"price": {
"type":"float"
},
"publisher": {
"type":"string"
}
}
}
}
}
}
]
}
}
屏幕截图
如果你想要要获取有关所有索引的信息,请使用 _all 或 * 代替索引名称。
删除索引
delete API 负责删除 elasticsearch 中存在的任何索引。每当您需要删除任何索引时,请将删除 API 与索引名称一起传递。
您还可以通过传递 _all 或 * 一次。
例如
DELETE http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
屏幕截图
打开/关闭索引
我们可以关闭一个索引一段时间或稍后再打开它。打开/关闭 API 允许我们对索引执行这些操作。封闭索引意味着索引被阻止进行读/写操作,并且除了维护其元数据外,集群上没有任何开销。可以随时打开此关闭的索引并启动正常过程。
请参阅以下步骤如何打开和关闭索引-
1.转到包含您创建的索引数量的概览标签。在这里,您将看到每个索引的两个下拉按钮信息和操作。
2、点击您想要关闭索引的操作按钮,然后点击关闭按钮。
3.点击确定按钮后会弹出一个弹出窗口。
4.现在,您将看到您选择的索引已关闭。
5.单击操作下拉列表以重新打开关闭的索引,然后单击打开按钮。
您还可以执行一些其他操作,例如删除、刷新或刷新索引使用这个接口。
6.将弹出一个屏幕以打开索引,点击确定按钮。
7.
索引存在
通过发送get请求,可以确定索引是否存在。如果 HTTP 响应为 202,则索引存在,如果返回响应为 Error 404,则该索引不存在。
也可以使用查询请求打开和关闭索引。请参阅下面的示例-
关闭索引
POST http://localhost/9200/book/
_close
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shards_acknowledge": true,
"indices": {
"book": {
"closed": true
}
}
}
屏幕截图
打开索引
POST http://localhost/9200/book/
_open
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shards_acknowledge": true,
}
屏幕截图
索引别名
此 API 允许我们为索引创建别名。要创建简单的别名,请使用 _alias 关键字,或者对于复杂的别名,请使用 _aliases。请记住,别名不能与索引名称相同。因此,始终为新别名提供不同的名称。请参阅下面给出的以下示例-
例如
复制代码
POST http://localhost/9200/
_aliases
{
"actions": [
{
"add": {
"index" : "book1",
"alias" : "book_pri"
}
}
]
}
通过执行此查询,我们将得到以下响应-
屏幕截图
获取别名
现在,您可以使用 GET 请求获取有关已创建别名的信息book_pri。执行以下代码以获取别名的所有信息-
Copy Code
GET http://localhost/9200/book_pri/
{ }
通过执行此查询,我们将得到以下响应-
{
"book1": {
"aliases": {
"book_pri": { }
},
"mappings": { },
"settings": {
"index": {
"creation_date": "1594989636357",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "2cFc6jUyS4C6tvM8yk2Sg",
"version": {
"created": "7080099"
},
"provided_name": "book1"
}
}
}
}
屏幕截图
删除别名
删除别名类似于向索引添加别名。我们只需要将remove关键字放在add的位置,然后执行请求即可。看看下面给出的例子-
Copy Code
POST http://localhost/9200/
_aliases
{
"actions": [
{
"remove": {
"index" : "book1",
"alias" : "book_pri"
}
}
]
}
通过执行此查询,我们将得到以下响应-
屏幕截图
分析
这个 API 帮助我们分析传入变量的文本。它打破文本字符串并返回带有标记值和偏移值(即开始偏移和结束偏移)的标记。在elasticsearch中,它允许用户在不指定任何索引的情况下进行分析。但是,我们也可以分析带有索引的文本,其中文本将根据与索引关联的分析器进行分析。
看下面的示例,不指定任何索引来分析文本-
复制代码
GET http://localhost/9200/
_analyze
{
"analyzer" : "standard",
"text" : "You are reading this text at lidihuo"
}
通过执行此查询,我们将得到以下响应-
{
"tokens": [
{
"token": "You",
"start_offset": 0,
"end_offset": 3,
"type": <ALPHANUM>,
"position": 0
},
{
"token": "are",
"start_offset": 4,
"end_offset": 7,
"type": <ALPHANUM>,
"position": 1
},
{
"token": "reading",
"start_offset": 8,
"end_offset": 15,
"type": <ALPHANUM>,
"position": 2
},
{
"token": "this",
"start_offset": 16,
"end_offset": 20,
"type": <ALPHANUM>,
"position": 3
},
{
"token": "text",
"start_offset": 21,
"end_offset": 25,
"type": <ALPHANUM>,
"position": 4
},
{
"token": "at",
"start_offset": 26,
"end_offset": 28,
"type": <ALPHANUM>,
"position": 5
},
{
"token": "lidihuo",
"start_offset": 29,
"end_offset": 39,
"type": <ALPHANUM>,
"position": 6
}
}
屏幕截图
索引模板
索引模板在创建索引时自动应用。我们还可以使用索引模板 API 为索引定义模板。见
Elasticsearch Index APIs
Elasticsearch 提供了 Index API 来管理索引的所有方面,例如索引模板、映射、别名和设置等。在 Elasticsearch,Index API 执行索引级别的操作。它负责管理不同的索引、索引设置、索引模板、映射、文件格式和别名。
当请求具有特定映射的索引时,此API 有助于添加或更新 JSON相应索引中的文档。它允许对索引执行许多操作,例如-创建、删除、分析、刷新、刷新等等。
有一个我们使用某些 API 对索引执行的操作列表。我们将通过示例详细讨论它们中的每一个:
创建索引
获取索引
删除索引
打开/关闭索引
存在索引
索引别名
分析
索引模板
索引设置
索引统计数据
冲洗
刷新
创建索引
该API用于创建索引。可以通过发送不带正文或具有适当映射、设置和别名的 PUT 请求来创建索引。每当用户将 JSON 对象传递给任何索引时,都会自动创建一个索引。
在下面的示例中,我们将创建一个名为 book 的索引。
例如
强>
POST http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
使用设置-
我们还可以在创建索引时在查询中添加一些设置,例如分片或副本-
POST http://localhost/9200/book/
{
"settings" : {
"index" : {
"number_of_shards" : 4,
"number_of_replicas" : 3
}
}
}
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
或者使用映射-
POST http://localhost/9200/book/_doc/
{
"settings": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 3
}
},
"mappings": {
"type1": {
"_source": {"enabled": false},
"properties": {
"book_name": {"type": "string"},
"author_name": {"type": "string"},
"published_date": {"type": "date"},
"price": {"type": "float"},
"publisher": {"type": "string"}
}
}
}
}
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shard_acknowledge": true,
"index": "book"
}
屏幕截图
注意: 请注意,这里我们使用 POST 方法创建带有映射的索引,而在早期的简单索引创建中,我们使用了 PUT 方法。
Get Index
Get API 用于提供有关您想要的特定索引的信息。它获取索引的信息。只需发送一个 GET 请求和索引名称即可调用此 API,该请求将返回有关该特定索引的所有信息。它通过 Get 请求返回元数据。看下面的例子
例如
GET http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
{
"took": 2084,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "_doc",
"_id": "dkqwVnMB1ug6i0GdSAsj",
"_score": 1,
"_source": {
"setting": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 3
}
},
"mapping": {
"type1": {
"_source": {
"enabled": false
},
"properties": {
"book_name": {
"type":"string"
},
"author_name": {
"type":"string"
},
"publish_date": {
"type":"date"
},
"price": {
"type":"float"
},
"publisher": {
"type":"string"
}
}
}
}
}
}
]
}
}
屏幕截图
如果你想要要获取有关所有索引的信息,请使用 _all 或 * 代替索引名称。
删除索引
delete API 负责删除 elasticsearch 中存在的任何索引。每当您需要删除任何索引时,请将删除 API 与索引名称一起传递。
您还可以通过传递 _all 或 * 一次。
例如
DELETE http://localhost/9200/book/
{ }
通过执行此查询,我们将得到以下响应-
屏幕截图
打开/关闭索引
我们可以关闭一个索引一段时间或稍后再打开它。打开/关闭 API 允许我们对索引执行这些操作。封闭索引意味着索引被阻止进行读/写操作,并且除了维护其元数据外,集群上没有任何开销。可以随时打开此关闭的索引并启动正常过程。
请参阅以下步骤如何打开和关闭索引-
1.转到包含您创建的索引数量的概览标签。在这里,您将看到每个索引的两个下拉按钮信息和操作。
2、点击您想要关闭索引的操作按钮,然后点击关闭按钮。
3.点击确定按钮后会弹出一个弹出窗口。
4.现在,您将看到您选择的索引已关闭。
5.单击操作下拉列表以重新打开关闭的索引,然后单击打开按钮。
您还可以执行一些其他操作,例如删除、刷新或刷新索引使用这个接口。
6.将弹出一个屏幕以打开索引,点击确定按钮。
7.
索引存在
通过发送get请求,可以确定索引是否存在。如果 HTTP 响应为 202,则索引存在,如果返回响应为 Error 404,则该索引不存在。
也可以使用查询请求打开和关闭索引。请参阅下面的示例-
关闭索引
POST http://localhost/9200/book/
_close
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shards_acknowledge": true,
"indices": {
"book": {
"closed": true
}
}
}
屏幕截图
打开索引
POST http://localhost/9200/book/
_open
{ }
通过执行此查询,我们将得到以下响应-
{
"acknowledge": true,
"shards_acknowledge": true,
}
屏幕截图
索引别名
此 API 允许我们为索引创建别名。要创建简单的别名,请使用 _alias 关键字,或者对于复杂的别名,请使用 _aliases。请记住,别名不能与索引名称相同。因此,始终为新别名提供不同的名称。请参阅下面给出的以下示例-
例如
复制代码
POST http://localhost/9200/
_aliases
{
"actions": [
{
"add": {
"index" : "book1",
"alias" : "book_pri"
}
}
]
}
通过执行此查询,我们将得到以下响应-
屏幕截图
获取别名
现在,您可以使用 GET 请求获取有关已创建别名的信息book_pri。执行以下代码以获取别名的所有信息-
Copy Code
GET http://localhost/9200/book_pri/
{ }
通过执行此查询,我们将得到以下响应-
{
"book1": {
"aliases": {
"book_pri": { }
},
"mappings": { },
"settings": {
"index": {
"creation_date": "1594989636357",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "2cFc6jUyS4C6tvM8yk2Sg",
"version": {
"created": "7080099"
},
"provided_name": "book1"
}
}
}
}
屏幕截图
删除别名
删除别名类似于向索引添加别名。我们只需要将remove关键字放在add的位置,然后执行请求即可。看看下面给出的例子-
Copy Code
POST http://localhost/9200/
_aliases
{
"actions": [
{
"remove": {
"index" : "book1",
"alias" : "book_pri"
}
}
]
}
通过执行此查询,我们将得到以下响应-
屏幕截图
分析
这个 API 帮助我们分析传入变量的文本。它打破文本字符串并返回带有标记值和偏移值(即开始偏移和结束偏移)的标记。在elasticsearch中,它允许用户在不指定任何索引的情况下进行分析。但是,我们也可以分析带有索引的文本,其中文本将根据与索引关联的分析器进行分析。
看下面的示例,不指定任何索引来分析文本-
复制代码
GET http://localhost/9200/
_analyze
{
"analyzer" : "standard",
"text" : "You are reading this text at lidihuo"
}
通过执行此查询,我们将得到以下响应-
{
"tokens": [
{
"token": "You",
"start_offset": 0,
"end_offset": 3,
"type": <ALPHANUM>,
"position": 0
},
{
"token": "are",
"start_offset": 4,
"end_offset": 7,
"type": <ALPHANUM>,
"position": 1
},
{
"token": "reading",
"start_offset": 8,
"end_offset": 15,
"type": <ALPHANUM>,
"position": 2
},
{
"token": "this",
"start_offset": 16,
"end_offset": 20,
"type": <ALPHANUM>,
"position": 3
},
{
"token": "text",
"start_offset": 21,
"end_offset": 25,
"type": <ALPHANUM>,
"position": 4
},
{
"token": "at",
"start_offset": 26,
"end_offset": 28,
"type": <ALPHANUM>,
"position": 5
},
{
"token": "lidihuo",
"start_offset": 29,
"end_offset": 39,
"type": <ALPHANUM>,
"position": 6
}
}
屏幕截图
索引模板
索引模板在创建索引时自动应用。我们还可以使用索引模板 API 为索引定义模板。见下面给出的例子-
复制代码
PUT http://localhost/9200/_template/template1
{
"template" : "te*",
"settings" : {
"number_of_shards" : 4
},
"mappings" : {
"type1" : {
"_source" : { "enabled" : false }
}
}
}
定义此模板后,任何以te开头的索引将与template1具有相同的设置。
索引设置
在 URL 末尾附加 _settings 关键字以获取索引设置。请参阅下面给出的示例-
复制代码
GET http://localhost/9200/book1/
_settings
{ }
通过执行此查询,我们将得到以下响应-
{
"book1": {
"settings": {
"index": {
"creation_date": "15949944338635",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "LisavEX7SAS5cKFIn3GTA",
"version": {
"created": "7080099"
},
"provided_name": "book"
}
}
}
}
屏幕截图
Index Stats
Stats 代表 Statistics。有时,我们需要分析特定索引的统计信息。此 API 有助于查找所需索引的统计信息。为此,我们只需要使用 GET 方法并在 URL 中附加 _stats 关键字和索引名称,然后执行查询。请参阅下面给出的示例-
复制代码
GET http://localhost/9200/book/
_stats
{ }
通过执行此查询,我们将得到以下响应-
{
"shard": {
"total": 2,
"successful": 1,
"failed": 0
},
"all": {
"primaries": {
"docs": {
"count": 1,
"deleted": 0
},
"store": {
"size_in_bytes": 7025
},
"indexing": {
"index_total": 0,
"index_time_in_millis": 0,
"index_current": 0,
"index_failed": 0,
"delete_total": 0,
"delete_time_in_millis": 0,
"delete_current": 0,
"noop_update_total": 0,
"is_throttled": false,
"throttle_time_in_millis": 0
}
.
.
.
.
"indicies": {
"book": {
"uuis": "LisavEX7SAS5cKFIn3GTS",
"primaries": {
"docs": {
"count": 1,
"deleted": 0
},
"store": {
"size_in_bytes": 7025
},
.
.
.
.
"recovery": {
"current_as_source": 0
"current_as_target": 0,
"throttle_time_in_millis": 0
}
}
}
}
屏幕截图
索引刷新
flush API 用于清理索引内存和内部事务日志。
它还会将数据迁移到索引存储。
要对索引执行此操作,请使用 _flush 关键字。基本上,它有助于从索引内存中刷新所有垃圾数据。
看下面给出的例子-
复制代码
GET http://localhost/9200/book/
_flush
{ }
通过执行此查询,我们将得到以下响应-
{
"shard": {
"total": 2,
"successful": 1,
"failed": 0
}
}
屏幕截图
索引刷新
在 elasticsearch 中,刷新是自动安排的,但您也可以使用此 API 手动刷新单个索引。
_refresh API 使所有数据都可用,直到上次执行操作为止。
它允许显式刷新一个或多个索引。指定要刷新的索引名称并在 URL 中附加 _refresh API 并执行查询请求。
不要忘记使用 GET 方法来成功执行查询。
看下面给出的例子-
复制代码
GET http://localhost/9200/book/
_refresh
{ }
通过执行此查询,我们将得到以下响应-
{
"shard": {
"total": 2,
"successful": 1,
"failed": 0
}
}
屏幕截图
这些是一些我们详细解释的许多操作都在 Index API 下。