-
Notifications
You must be signed in to change notification settings - Fork 35
[CCI] Create advanced index actions guide #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
776d31a
752799a
044fafd
dfd6cf7
9519a0d
de375bb
6ff32af
2315a1e
70b587d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # Advanced Index Actions | ||
|
|
||
| In this guide, we will look at some advanced index actions that are not covered in the [Index Lifecycle](index_lifecycle.md) guide. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That file doesn't exist yet, so the link checker breaks. You can combine all the guides in 1 PR or make them in order they link to each-other.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dblock Can we wait for the index lifecycle to merge first and then re-run the link checker? |
||
|
|
||
| ## Setup | ||
|
|
||
| Let's create a client instance, and an index named `movies`: | ||
|
|
||
| ```rust | ||
| // Create a client to make API calls to OpenSearch running on http://localhost:9200. | ||
| let client = OpenSearch::default(); | ||
| // Alternatively, you can create a client to make API calls against OpenSearch running on a specific url::Url. | ||
| let url = Url::parse("https://example.com")?; | ||
| let transport = TransportBuilder::new(SingleNodeConnectionPool::new(url)).cert_validation(CertificateValidation::None).build()?; | ||
| let client = OpenSearch::new(transport); | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| client.indices().create(IndicesCreateParts::Index("movies")).send().await?; | ||
| ``` | ||
|
|
||
| ## API Actions | ||
|
|
||
| ### Clear index cache | ||
|
|
||
| You can clear the cache of an index or indices by using the `indices.clear_cache` API action. The following example clears the cache of the `movies` index: | ||
|
|
||
| ```rust | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).send().await?; | ||
| ``` | ||
|
|
||
| By default, the `indices.clear_cache` API action clears all types of cache. To clear specific types of cache pass the the `query`, `fielddata`, or `request` parameter to the API action: | ||
|
|
||
| ```rust | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).query(true).send().await?; | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).fielddata(true).request(true).send().await?; | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### Flush index | ||
|
|
||
| Sometimes you might want to flush an index or indices to make sure that all data in the transaction log is persisted to the index. To flush an index or indices use the `indices.flush` API action. The following example flushes the `movies` index: | ||
|
|
||
| ```rust | ||
| client.indices().flush(IndicesFlushParts::Index(&["movies"])).send().await?; | ||
| ``` | ||
|
|
||
| ### Refresh index | ||
|
|
||
| You can refresh an index or indices to make sure that all changes are available for search. To refresh an index or indices use the `indices.refresh` API action: | ||
|
|
||
| ```rust | ||
| client.indices().refresh(IndicesRefreshParts::Index(&["movies"])).send().await?; | ||
| ``` | ||
|
|
||
| ### Open/Close index | ||
|
|
||
| You can close an index to prevent read and write operations on the index. A closed index does not have to maintain certain data structures that an opened index require, reducing the memory and disk space required by the index. The following example closes and reopens the `movies` index: | ||
|
|
||
| ```rust | ||
| client.indices().close(IndicesCloseParts::Index(&["movies"])).send().await?; | ||
| client.indices().open(IndicesOpenParts::Index(&["movies"])).send().await?; | ||
| ``` | ||
|
|
||
| ### Force merge index | ||
|
|
||
| You can force merge an index or indices to reduce the number of segments in the index. This can be useful if you have a large number of small segments in the index. Merging segments reduces the memory footprint of the index. Do note that this action is resource intensive and it is only recommended for read-only indices. The following example force merges the `movies` index: | ||
|
|
||
| ```rust | ||
| client.indices().forcemerge(IndicesForcemergeParts::Index(&["movies"])).send().await?; | ||
| ``` | ||
|
|
||
| ### Clone index | ||
|
|
||
| You can clone an index to create a new index with the same mappings, data, and MOST of the settings. The source index must be in read-only state for cloning. The following example blocks write operations from `movies` index, clones the said index to create a new index named `movies_clone`, then re-enables write: | ||
|
|
||
| ```rust | ||
| client.indices().add_block(IndicesAddBlockParts::IndexBlock(&["movies"],"write")).send().await?; | ||
| client.indices().clone(IndicesCloneParts::IndexTarget("movies","movies_clone")).send().await?; | ||
| client.indices().put_settings(IndicesPutSettingsParts::Index(&["movies"])) | ||
| .body(json!({ | ||
| "index": { | ||
| "blocks": { | ||
| "write": false | ||
| } | ||
| } | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| })).send().await?; | ||
| ``` | ||
|
|
||
| ### Split index | ||
|
|
||
| You can split an index into another index with more primary shards. The source index must be in read-only state for splitting. The following example create the read-only `books` index with 30 routing shards and 5 shards (which is divisible by 30), splits index into `bigger_books` with 10 shards (which is also divisible by 30), then re-enables write: | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```rust | ||
| client.indices().create(IndicesCreateParts::Index("books")) | ||
| .body(json!({ | ||
| "settings": { | ||
| "index": { | ||
| "number_of_shards": 5, | ||
| "number_of_routing_shards": 30, | ||
| "blocks": { | ||
| "write": true | ||
| } | ||
| } | ||
| } | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| })).send().await?; | ||
| client.indices().split(IndicesSplitParts::IndexTarget("books","bigger_books")).body(json!({"settings": {"index": {"number_of_shards": 10}}})).send().await?; | ||
| client.indices().put_settings(IndicesPutSettingsParts::Index(&["books"])).body(json!({"index": {"blocks": {"write": false}}})).send().await?; | ||
| ``` | ||
|
|
||
| ## Cleanup | ||
|
|
||
| Let's delete all the indices we created in this guide: | ||
|
|
||
| ```rust | ||
| client.indices().delete(IndicesDeleteParts::Index(&["movies", "books", "movies_clone", "bigger_books"])).send().await?; | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| use opensearch::{ http::transport::TransportBuilder, OpenSearch, http::transport::SingleNodeConnectionPool, cert::CertificateValidation}; | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| use url::Url; | ||
| use opensearch::indices::{ IndicesCreateParts, IndicesClearCacheParts, IndicesFlushParts, IndicesRefreshParts, IndicesCloseParts, IndicesOpenParts, IndicesForcemergeParts, IndicesAddBlockParts, IndicesCloneParts, IndicesSplitParts, IndicesPutSettingsParts,IndicesDeleteParts}; | ||
| use serde_json::json; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| // Let's create a client instance, and an index named `movies`: | ||
|
|
||
| // Create a client to make API calls to OpenSearch running on http://localhost:9200. | ||
| let _client = OpenSearch::default(); | ||
| // Alternatively, you can create a client to make API calls against OpenSearch running on a specific url::Url. | ||
| let url = Url::parse("https://example.com")?; | ||
| let transport = TransportBuilder::new(SingleNodeConnectionPool::new(url)).cert_validation(CertificateValidation::None).build()?; | ||
| let client = OpenSearch::new(transport); | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| client.indices().create(IndicesCreateParts::Index("movies")).send().await?; | ||
| // You can clear the cache of an index or indices by using the `indices.clear_cache` API action. The following example clears the cache of the `movies` index: | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).send().await?; | ||
| // By default, the `indices.clear_cache` API action clears all types of cache. To clear specific types of cache pass the the `query`, `fielddata`, or `request` parameter to the API action: | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).query(true).send().await?; | ||
| client.indices().clear_cache(IndicesClearCacheParts::Index(&["movies"])).fielddata(true).request(true).send().await?; | ||
| // Sometimes you might want to flush an index or indices to make sure that all data in the transaction log is persisted to the index. To flush an index or indices use the `indices.flush` API action. The following example flushes the `movies` index: | ||
| client.indices().flush(IndicesFlushParts::Index(&["movies"])).send().await?; | ||
| // You can refresh an index or indices to make sure that all changes are available for search. To refresh an index or indices use the `indices.refresh` API action: | ||
| client.indices().refresh(IndicesRefreshParts::Index(&["movies"])).send().await?; | ||
| // You can close an index to prevent read and write operations on the index. A closed index does not have to maintain certain data structures that an opened index require, reducing the memory and disk space required by the index. The following example closes and reopens the `movies` index: | ||
| client.indices().close(IndicesCloseParts::Index(&["movies"])).send().await?; | ||
| client.indices().open(IndicesOpenParts::Index(&["movies"])).send().await?; | ||
| // You can force merge an index or indices to reduce the number of segments in the index. This can be useful if you have a large number of small segments in the index. Merging segments reduces the memory footprint of the index. Do note that this action is resource intensive and it is only recommended for read-only indices. The following example force merges the `movies` index: | ||
| client.indices().forcemerge(IndicesForcemergeParts::Index(&["movies"])).send().await?; | ||
| // You can clone an index to create a new index with the same mappings, data, and MOST of the settings. The source index must be in read-only state for cloning. The following example blocks write operations from `movies` index, clones the said index to create a new index named `movies_clone`, then re-enables write: | ||
| client.indices().add_block(IndicesAddBlockParts::IndexBlock(&["movies"],"write")).send().await?; | ||
| client.indices().clone(IndicesCloneParts::IndexTarget("movies","movies_clone")).send().await?; | ||
| client.indices().put_settings(IndicesPutSettingsParts::Index(&["movies"])) | ||
| .body(json!({ | ||
| "index": { | ||
| "blocks": { | ||
| "write": false | ||
| } | ||
| } | ||
| })).send().await?; | ||
| // You can split an index into another index with more primary shards. The source index must be in read-only state for splitting. The following example create the read-only `books` index with 30 routing shards and 5 shards (which is divisible by 30), splits index into `bigger_books` with 10 shards (which is also divisible by 30), then re-enables write: | ||
Nicksqain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| client.indices().create(IndicesCreateParts::Index("books")) | ||
| .body(json!({ | ||
| "settings": { | ||
| "index": { | ||
| "number_of_shards": 5, | ||
| "number_of_routing_shards": 30, | ||
| "blocks": { | ||
| "write": true | ||
| } | ||
| } | ||
| } | ||
| })).send().await?; | ||
| client.indices().split(IndicesSplitParts::IndexTarget("books","bigger_books")).body(json!({"settings": {"index": {"number_of_shards": 10}}})).send().await?; | ||
| client.indices().put_settings(IndicesPutSettingsParts::Index(&["books"])).body(json!({"index": {"blocks": {"write": false}}})).send().await?; | ||
| // Let's delete all the indices we created in this guide: | ||
| client.indices().delete(IndicesDeleteParts::Index(&["movies", "books", "movies_clone", "bigger_books"])).send().await?; | ||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.