Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

chore: script for add alias to existing index #346

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions scripts/elasticsearch-operations.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// Running ES Script
// ACCESS_KEY=<ACCESS_KEY> SECRET_KEY=<SECRET_KEY> ES_DOMAIN_ENDPOINT=<ES_DOMAIN_ENDPOINT> node elasticsearch-operations.js <region> <function to execute> <optional additional params>
//
Expand Down Expand Up @@ -110,6 +110,43 @@ const deleteIndex = async () => {
}
};

const addAlias = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a comment/docs describing what this function does.

I know that the rest of this file has almost no documentation, but I feel like this one deserves it since It will be mentioned as the path to upgrade for breaking change on the release notes.

All the other functions are extras that I'm not sure if they are ever used (I didn't even know this file existed).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll add documentation for it in subsequent PR. I was not aware of this file exited before either, I'll bring it up in tmr's standup to see if we still want to keep them.

// Get all indices
const response = await es.cat.indices({ format: 'json' });
const indices = response.body.map(indexDetail => indexDetail.index);

// Check indices for alias
const checkAlias = indices.map(indexName => {
return es.indices.existsAlias({
index: indexName,
name: `${indexName}-alias`,
});
});
const checkAliasResponse = await Promise.all(checkAlias);

// Filter out indices without alias
const aliasesToAdd = checkAliasResponse
.map((checkAliasResult, index) => {
return { hasAlias: checkAliasResult.body, indexName: indices[index] };
})
.filter(checkResult => !checkResult.hasAlias);

// Add alias if needed
if (aliasesToAdd.length === 0) {
console.log('All indices have alias created, nothing to do.');
} else {
console.log(
`Adding aliases for indices: `,
aliasesToAdd.map(aliasToAdd => aliasToAdd.indexName),
);
const addAliases = aliasesToAdd.map(checkResult => {
return es.indices.putAlias({ index: checkResult.indexName, name: `${checkResult.indexName}-alias` });
});
await Promise.all(addAliases);
console.log('Aliases added.');
}
};

const functionToExecute = process.argv[3];
if (functionToExecute === 'search') {
const name = process.argv[4];
Expand All @@ -122,4 +159,6 @@ if (functionToExecute === 'search') {
deleteIndex();
} else if (functionToExecute === 'getIndexMapping') {
getIndexMapping();
} else if (functionToExecute === 'addAlias') {
addAlias();
}