Skip to content

Commit 478f251

Browse files
committed
Script to execute Optimize(Forcemerge) operation on ES indices
1 parent c7c9951 commit 478f251

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Mandatory=$True, HelpMessage="Enter the Elasticsearch server Url. eg: http://localhost:9200/")]
4+
[string]$ElasticServerUrl,
5+
6+
[Parameter(Mandatory=$True, HelpMessage="Enter index name. Eg: code* for all code indices or a specific index name")]
7+
[string]$IndexName
8+
)
9+
10+
function OptimizeIndex
11+
{
12+
$optimizeCommand = $ElasticServerUrl + $IndexName + "/_forcemerge?only_expunge_deletes=true"
13+
$response = Invoke-RestMethod $optimizeCommand -Method Post -ContentType "application/json" -Credential (Get-Credential)
14+
}
15+
16+
Write-Host -ForegroundColor Red @"
17+
The optimize index operation you are going to execute will expunge all deleted documents of a given index.
18+
This operation could cause very large shards to remain on disk until all the documents present in the index are just deleted documents.
19+
Proceed with this operation if and only if it is absolutely required.
20+
"@
21+
22+
Write-Host "Do you want to continue - Yes or No? " -NoNewline -ForegroundColor Magenta
23+
$userInput = Read-Host
24+
25+
if($userInput -like "Yes")
26+
{
27+
Write-Host "Starting optimize index operation.." -ForegroundColor Green
28+
OptimizeIndex
29+
Write-Host "Initiated optimize operation.This operation executes at the background and will auto complete." -ForegroundColor Green
30+
31+
}
32+
else
33+
{
34+
Write-Warning "Exiting! No optimize operation was performed for the given index." -ForegroundColor Cyan
35+
}

TFS_2018RTW/OptimizeElasticIndex.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Mandatory=$True, HelpMessage="Enter the Elasticsearch server Url. eg: http://localhost:9200/")]
4+
[string]$ElasticServerUrl,
5+
6+
[Parameter(Mandatory=$True, HelpMessage="Enter index name. Eg: code* for all code indices or a specific index name")]
7+
[string]$IndexName
8+
)
9+
10+
function OptimizeIndex
11+
{
12+
$optimizeCommand = $ElasticServerUrl + $IndexName + "/_forcemerge?only_expunge_deletes=true"
13+
$response = Invoke-RestMethod $optimizeCommand -Method Post -ContentType "application/json" -Credential (Get-Credential)
14+
}
15+
16+
Write-Host -ForegroundColor Red @"
17+
The optimize index operation you are going to execute will expunge all deleted documents of a given index.
18+
This operation could cause very large shards to remain on disk until all the documents present in the index are just deleted documents.
19+
Proceed with this operation if and only if it is absolutely required.
20+
"@
21+
22+
Write-Host "Do you want to continue - Yes or No? " -NoNewline -ForegroundColor Magenta
23+
$userInput = Read-Host
24+
25+
if($userInput -like "Yes")
26+
{
27+
Write-Host "Starting optimize index operation.." -ForegroundColor Green
28+
OptimizeIndex
29+
Write-Host "Initiated optimize operation.This operation executes at the background and will auto complete." -ForegroundColor Green
30+
31+
}
32+
else
33+
{
34+
Write-Warning "Exiting! No optimize operation was performed for the given index." -ForegroundColor Cyan
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Mandatory=$True, HelpMessage="Enter the Elasticsearch server Url. eg: http://localhost:9200/")]
4+
[string]$ElasticServerUrl,
5+
6+
[Parameter(Mandatory=$True, HelpMessage="Enter index name. Eg: code* for all code indices or a specific index name")]
7+
[string]$IndexName
8+
)
9+
10+
function OptimizeIndex
11+
{
12+
$optimizeCommand = $ElasticServerUrl + $IndexName + "/_forcemerge?only_expunge_deletes=true"
13+
$response = Invoke-RestMethod $optimizeCommand -Method Post -ContentType "application/json" -Credential (Get-Credential)
14+
}
15+
16+
Write-Host -ForegroundColor Red @"
17+
The optimize index operation you are going to execute will expunge all deleted documents of a given index.
18+
This operation could cause very large shards to remain on disk until all the documents present in the index are just deleted documents.
19+
Proceed with this operation if and only if it is absolutely required.
20+
"@
21+
22+
Write-Host "Do you want to continue - Yes or No? " -NoNewline -ForegroundColor Magenta
23+
$userInput = Read-Host
24+
25+
if($userInput -like "Yes")
26+
{
27+
Write-Host "Starting optimize index operation.." -ForegroundColor Green
28+
OptimizeIndex
29+
Write-Host "Initiated optimize operation.This operation executes at the background and will auto complete." -ForegroundColor Green
30+
31+
}
32+
else
33+
{
34+
Write-Warning "Exiting! No optimize operation was performed for the given index." -ForegroundColor Cyan
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Mandatory=$True, HelpMessage="Enter the Elasticsearch server Url. eg: http://localhost:9200/")]
4+
[string]$ElasticServerUrl,
5+
6+
[Parameter(Mandatory=$True, HelpMessage="Enter index name. Eg: code* for all code indices or a specific index name")]
7+
[string]$IndexName
8+
)
9+
10+
function OptimizeIndex
11+
{
12+
$optimizeCommand = $ElasticServerUrl + $IndexName + "/_forcemerge?only_expunge_deletes=true"
13+
$response = Invoke-RestMethod $optimizeCommand -Method Post -ContentType "application/json" -Credential (Get-Credential)
14+
}
15+
16+
Write-Host -ForegroundColor Red @"
17+
The optimize index operation you are going to execute will expunge all deleted documents of a given index.
18+
This operation could cause very large shards to remain on disk until all the documents present in the index are just deleted documents.
19+
Proceed with this operation if and only if it is absolutely required.
20+
"@
21+
22+
Write-Host "Do you want to continue - Yes or No? " -NoNewline -ForegroundColor Magenta
23+
$userInput = Read-Host
24+
25+
if($userInput -like "Yes")
26+
{
27+
Write-Host "Starting optimize index operation.." -ForegroundColor Green
28+
OptimizeIndex
29+
Write-Host "Initiated optimize operation.This operation executes at the background and will auto complete." -ForegroundColor Green
30+
31+
}
32+
else
33+
{
34+
Write-Warning "Exiting! No optimize operation was performed for the given index." -ForegroundColor Cyan
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[CmdletBinding()]
2+
Param(
3+
[Parameter(Mandatory=$True, HelpMessage="Enter the Elasticsearch server Url. eg: http://localhost:9200/")]
4+
[string]$ElasticServerUrl,
5+
6+
[Parameter(Mandatory=$True, HelpMessage="Enter index name. Eg: code* for all code indices or a specific index name")]
7+
[string]$IndexName
8+
)
9+
10+
function OptimizeIndex
11+
{
12+
$optimizeCommand = $ElasticServerUrl + $IndexName + "/_forcemerge?only_expunge_deletes=true"
13+
$response = Invoke-RestMethod $optimizeCommand -Method Post -ContentType "application/json" -Credential (Get-Credential)
14+
}
15+
16+
Write-Host -ForegroundColor Red @"
17+
The optimize index operation you are going to execute will expunge all deleted documents of a given index.
18+
This operation could cause very large shards to remain on disk until all the documents present in the index are just deleted documents.
19+
Proceed with this operation if and only if it is absolutely required.
20+
"@
21+
22+
Write-Host "Do you want to continue - Yes or No? " -NoNewline -ForegroundColor Magenta
23+
$userInput = Read-Host
24+
25+
if($userInput -like "Yes")
26+
{
27+
Write-Host "Starting optimize index operation.." -ForegroundColor Green
28+
OptimizeIndex
29+
Write-Host "Initiated optimize operation.This operation executes at the background and will auto complete." -ForegroundColor Green
30+
31+
}
32+
else
33+
{
34+
Write-Warning "Exiting! No optimize operation was performed for the given index." -ForegroundColor Cyan
35+
}

0 commit comments

Comments
 (0)