乐闻世界logo
搜索文章和话题

How do you create, delete, list, and query indices in Elasticsearch?

1个答案

1

Creating Indices

In Elasticsearch, we can create a new index by sending an HTTP PUT request to the server. For example, to create an index named 'my_index', we can do the following:

bash
PUT /my_index { "settings": { "number_of_shards": 1, "number_of_replicas": 2 }, "mappings": { "properties": { "name": { "type": "text" }, "age": { "type": "integer" } } } }

Here, we define settings and mappings. The number_of_shards specifies the number of shards, and number_of_replicas specifies the number of replicas. The mappings define the data types for the index, such as 'text' and 'integer'.

Deleting Indices

To delete an index, we send an HTTP DELETE request. For example, to delete the 'my_index' index created above, we can do the following:

bash
DELETE /my_index

This operation permanently deletes the index and all its data, which is irreversible. Therefore, it is crucial to consider carefully before execution.

Listing Indices

To list all indices, we send a GET request to the /_cat/indices endpoint. For example:

bash
GET /_cat/indices?v

This request returns a list of all indices in Elasticsearch, including their health status and names.

Querying Indices

Querying data in Elasticsearch can be performed in various ways, with the most common approach being the use of Elasticsearch's Query DSL (Domain Specific Language). For example, to query documents where the 'name' field contains 'John' in the 'my_index' index, we can do the following:

bash
GET /my_index/_search { "query": { "match": { "name": "John" } } }

Here, the match query searches for documents matching the criteria in the 'my_index' index. These basic operations demonstrate how to manage and query indices in Elasticsearch, which are highly useful for daily data retrieval and index management.

2024年8月13日 13:32 回复

你的答案