Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common operations of ElasticSearch: index articles

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

[TOC]

0 description

Based on es 5.4,5.6, refer to two materials, "from Lucene to Elasticsearch full-text Retrieval practice" and official documents

Https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices.html (the official documentation is too good to be missed! ).

1 create index PUT my_index

Note1: indexes cannot have uppercase letters

By default, Note2:es sets 5 shards and 1 copy for the index.

NOte3: the number of index fragments cannot be modified once specified, but the number of copies can be modified at any time through the command.

You can add a settings configuration:

PUT my_index {"settings": {"number_of_shards": 3, "number_of_replicas": 1}} 2 Update the number of index copies PUT my_index/_settings {"number_of_replicas": 2} 3 read and write permission settings

The permission parameters are as follows:

Parameter setting indicates that when blocks.read_only:true is true, the current index is only allowed to read or write, or when blocks.read:true is updated to true, read operation to the current index is prohibited when blocks.write:true is true, write operation to the current index is prohibited

For example, to prohibit users from writing:

PUT my_index/_settings {"blocks.write": true}

When the data is written again, a 403 error is returned.

Resume the write operation:

PUT my_index/_settings {"blocks.write": false} 4 View Index GET my_index/_mapping

Return the result:

{"my_index": {"mappings": {"my_type": {"properties": {"title": {"type": "text", "fields": {"keyword": {"type": "keyword" "ignore_above": 256}}

View setting information for multiple indexes at the same time:

GET my_index,my_index2/_mapping

View the setting information of all indexes in the cluster:

GET _ all/_settings5 delete index DELETE my_index

If the deleted index does not exist, it will report that the index did not find an exception.

6 opening and closing of the index

When the index is turned off, it takes up almost no system resources.

POST my_index/_close

Close multiple indexes:

POST my_index,my_index2/_close

Add the ignore_unavailable parameter:

POST my_index,my_index2,my_index3/_close?ignore_unavailable=true

My_index3 does not exist, and if you do not add the ignore_unavailable parameter, the index will be thrown without errors.

Turn off all indexes in the cluster:

POST _ all/_close

Close the index as a matchable, and close the index that starts with test:

POST test*/_close7 replication index POST _ reindex {"source": {"index": "my_index"}, "dest": {"index": "my_index3"}}

Note1: the target index does not copy the configuration information in the source index. You need to set the number of shards, copies and other information of the target index before the _ redinx operation. If it is not set, or there is no my_index3, a new index will be created and the default configuration information will be used.

Note2:_reindex is actually used to copy indexed documents, so if there are no documents in my_index, the my_index3 will not be created.

You can add type and query to source to restrict copied documents:

POST _ reindex {"source": {"index": "my_index", "type": "my_type", "query": {"term": {"title": "elasticsearch"}, "dest": {"index": "my_index3"} 8 shrink the index

Refer directly to the official documentation: https://www.elastic.co/guide/en/elasticsearch/reference/5.4/indices-shrink-index.html, very detailed.

The shrink index API allows you to shrink an existing index into a new index with fewer primary shards. The requested number of primary shards in the target index must be a factor of the number of shards in the source index. For example an index with 8 primary shards can be shrunk into 4, 2or 1 primary shards or an index with 15 primary shards can be shrunk into 5, 3 or 1. If the number of shards in the index is a prime number it can only be shrunk into a single primary shard. Before shrinking, a (primary or replica) copy of every shard in the index must be present on the same node.

Shrinking works as follows:

First, it creates a new target index with the same definition as the source index, but with a smaller number of primary shards.Then it hard-links segments from the source index into the target index. (If the file system doesn't support hard-linking, then all segments are copied into the new index, which is a much more time consuming process.) Finally, it recovers the target index as though it were a closed index which had just been re-opened.

Preparation before shrinking the index:

PUT / my_source_index/_settings {"settings": {"index.routing.allocation.require._name": "shrink_node_name", "index.blocks.write": true}}

To shrink the index:

POST my_source_index/_shrink/my_target_index

You can also add some other configuration information:

POST my_source_index/_shrink/my_target_index {"settings": {"index.number_of_replicas": 1, "index.number_of_shards": 1, "index.codec": "best_compression"}, "aliases": {"my_search_indices": {}

If you don't understand, be sure to read the links to the official documents provided above.

9 index alias

Create an index alias:

POST _ aliases {"actions": [{"add": {"index": "test1", "alias": "alias1"}]}

Remove index aliases:

POST _ aliases {"actions": [{"remove": {"index": "test1", "alias": "alias1"}]}

Note1: an index can have multiple aliases (add multiple times), and an alias can correspond to multiple indexes (multiple times is fine)

Note2: when using aliases, note that if aliases and indexes are one-to-one, it is possible to index aliases or query documents according to ID, but if aliases and indexes are one-to-many, using aliases will cause errors because Elasticsearch does not know which index to write or read the document from.

View the alias of an index:

GET my_index3/_aliases result: {"my_index3": {"aliases": {"alias_test": {}, "alias_test2": {}

View the index corresponding to an alias:

GET alias_test/_aliases result: {"my_index3": {"aliases": {"alias_test": {}, "alias_test2": {}}, "my_index2": {"aliases": {"alias_test": {}, "my_index": {"aliases": {"alias_test": {}

View all available aliases on the cluster:

GET _ all/_aliases or GET _ aliases

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report