Elasticsearch 简明教程

Elasticsearch - Index APIs

这些 API 负责管理索引的所有方面,例如设置、别名、映射、索引模板。

These APIs are responsible for managing all the aspects of the index like settings, aliases, mappings, index templates.

Create Index

此 API 可帮助您创建索引。可以在用户向任何索引传递 JSON 对象时或在用户这么做之前自动创建索引。要创建索引,您只需发送一个包含设置、映射和别名(或只是一个不带正文的简单请求)的 PUT 请求。

This API helps you to create an index. An index can be created automatically when a user is passing JSON objects to any index or it can be created before that. To create an index, you just need to send a PUT request with settings, mappings and aliases or just a simple request without body.

PUT colleges

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

{
   "acknowledged" : true,
   "shards_acknowledged" : true,
   "index" : "colleges"
}

我们还可以向上述命令添加一些设置:

We can also add some settings to the above command −

PUT colleges
{
  "settings" : {
      "index" : {
         "number_of_shards" : 3,
         "number_of_replicas" : 2
      }
   }
}

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

{
   "acknowledged" : true,
   "shards_acknowledged" : true,
   "index" : "colleges"
}

Delete Index

此 API 可帮助您删除任何索引。您只需传递一个带有特定索引名称的删除请求。

This API helps you to delete any index. You just need to pass a delete request with the name of that particular Index.

DELETE /colleges

您只需使用 _all 或 * 即可删除所有索引。

You can delete all indices by just using _all or *.

Get Index

只需向一个或多个索引发送获取请求即可调用此 API。这将返回有关索引的信息。

This API can be called by just sending get request to one or more than one indices. This returns the information about index.

GET colleges

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

{
   "colleges" : {
      "aliases" : {
         "alias_1" : { },
         "alias_2" : {
            "filter" : {
               "term" : {
                  "user" : "pkay"
               }
            },
            "index_routing" : "pkay",
            "search_routing" : "pkay"
         }
      },
      "mappings" : { },
      "settings" : {
         "index" : {
            "creation_date" : "1556245406616",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "3ExJbdl2R1qDLssIkwDAug",
            "version" : {
               "created" : "7000099"
            },
            "provided_name" : "colleges"
         }
      }
   }
}

通过使用 _all 或 * 您可以获取所有索引的信息。

You can get the information of all the indices by using _all or *.

Index Exist

只需向该索引发送获取请求即可确定索引是否存在。如果 HTTP 响应为 200,则表示索引存在;如果为 404,则表示索引不存在。

Existence of an index can be determined by just sending a get request to that index. If the HTTP response is 200, it exists; if it is 404, it does not exist.

HEAD colleges

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

200-OK

Index Settings

您只需在 URL 最后追加 _settings 关键字即可获取索引设置。

You can get the index settings by just appending _settings keyword at the end of URL.

GET /colleges/_settings

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

{
   "colleges" : {
      "settings" : {
         "index" : {
            "creation_date" : "1556245406616",
            "number_of_shards" : "1",
            "number_of_replicas" : "1",
            "uuid" : "3ExJbdl2R1qDLssIkwDAug",
            "version" : {
               "created" : "7000099"
            },
            "provided_name" : "colleges"
         }
      }
   }
}

Index Stats

此 API 可帮助您提取特定索引的统计信息。您只需发送一个带有索引 URL 和 _stats 关键字的获取请求(位于最后)。

This API helps you to extract statistics about a particular index. You just need to send a get request with the index URL and _stats keyword at the end.

GET /_stats

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

………………………………………………
},
   "request_cache" : {
      "memory_size_in_bytes" : 849,
      "evictions" : 0,
      "hit_count" : 1171,
      "miss_count" : 4
   },
   "recovery" : {
      "current_as_source" : 0,
      "current_as_target" : 0,
      "throttle_time_in_millis" : 0
   }
} ………………………………………………

Flush

索引的刷新过程确保当前仅保存在事务日志中的任何数据也永久保存在 Lucene 中。这减少了恢复时间,因为在打开 Lucene 索引后,无需从事务日志重新对该数据建立索引。

The flush process of an index makes sure that any data that is currently only persisted in the transaction log is also permanently persisted in Lucene. This reduces recovery times as that data does not need to be reindexed from the transaction logs after the Lucene indexed is opened.

POST colleges/_flush

运行上述代码后,我们得到如下所示的输出:

On running the above code, we get the output as shown below −

{
   "_shards" : {
      "total" : 2,
      "successful" : 1,
      "failed" : 0
   }
}