Skip to content
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

fix(setupIndex): fix case where index already exists #219

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 16 additions & 8 deletions src/libs/opensearch-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,33 @@ export async function getItem(host:string, index:string, id:string){
}
}

export async function createIndexIfNotExists(host:string, index:string) {
export async function indexExists(host:string, index:string) {
client = client || (await getClient(host));
try {
const indexExists = await client.indices.exists({ index, });
if (!indexExists.body) {
const createResponse = await client.indices.create({
index,
});

console.log('Index created:', createResponse);
if (indexExists.body) {
return true;
} else {
console.log('Index already exists.');
return false;
}
} catch (error) {
console.error('Error creating index:', error);
throw error;
}
}

export async function createIndex(host:string, index:string) {
client = client || (await getClient(host));
try {
const createResponse = await client.indices.create({
index,
});
} catch (error) {
console.error('Error creating index:', error);
throw error;
}
}

export async function updateFieldMapping(host:string, index:string, properties: object) {
client = client || (await getClient(host));
try {
Expand Down
35 changes: 18 additions & 17 deletions src/services/data/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@
}
};

export const handler: Handler = async (event) => {

Check warning on line 23 in src/services/data/handlers/index.ts

View workflow job for this annotation

GitHub Actions / lint

'event' is defined but never used
await manageIndex();
};

async function manageIndex() {
try {
const createIndexReponse = await os.createIndexIfNotExists(
process.env.osDomain,
"main"
);
console.log(createIndexReponse);

const updateFieldMappingResponse = await os.updateFieldMapping(
process.env.osDomain,
"main",
{
rais: {
type: "object",
enabled: false,
},
}
);
console.log(updateFieldMappingResponse);
if (!(await os.indexExists(process.env.osDomain, "main"))) {
const createIndexReponse = await os.createIndex(
process.env.osDomain,
"main"
);
console.log(createIndexReponse);
const updateFieldMappingResponse = await os.updateFieldMapping(
process.env.osDomain,
"main",
{
rais: {
type: "object",
enabled: false,
},
}
);
console.log(updateFieldMappingResponse);
}
} catch (error) {
console.log(error);
throw "ERROR: Error occured during index management.";
Expand Down
Loading