Skip to content

Commit

Permalink
feat: modify elasticsearch index creation (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Oct 20, 2023
1 parent b53a4ad commit ed3ef6a
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 198 deletions.
1 change: 1 addition & 0 deletions packages/api-elasticsearch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from "./compression";
export * from "./operators";
export * from "./cursors";
export * from "./client";
export * from "./utils";
export { createGzipCompression } from "./plugins/GzipCompression";

/**
Expand Down
122 changes: 122 additions & 0 deletions packages/api-elasticsearch/src/utils/createIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Client } from "@elastic/elasticsearch";
import { PluginsContainer } from "@webiny/plugins";
import { getLastAddedIndexPlugin } from "~/indices";
import { ElasticsearchIndexPlugin } from "~/plugins";
import WebinyError from "@webiny/error";

interface OnExists {
(): void;
}

interface OnError {
(ex: Error): Error;
}

interface ExistsIndexParams {
client: Client;
index: string;
onExists?: OnExists;
}

const indexExists = async (params: ExistsIndexParams): Promise<boolean> => {
const { client, index, onExists } = params;

try {
const response = await client.indices.exists({
index,
ignore_unavailable: false,
allow_no_indices: true,
include_defaults: true,
flat_settings: false,
local: false
});
if (!response.body) {
return false;
}
if (onExists) {
onExists();
}
return true;
} catch (ex) {
console.error(`Could not determine if the index "${index}" exists.`);
}
return false;
};

interface IndexCreateParams {
client: Client;
index: string;
type: string;
tenant: string;
locale: string;
plugin: ElasticsearchIndexPlugin;
onError?: OnError;
}

const indexCreate = async (params: IndexCreateParams): Promise<void> => {
const { client, index, plugin, tenant, locale, type, onError } = params;

try {
await client.indices.create({
index,
body: {
...plugin.body
}
});
} catch (ex) {
let error = ex;
if (onError) {
error = onError(ex);
}
throw new WebinyError(
error.message || `Could not create Elasticsearch index for the ${type}.`,
error.code || "CREATE_ELASTICSEARCH_INDEX_ERROR",
{
error: {
...error,
message: error.message,
code: error.code,
data: error.data
},
type,
locale,
tenant,
index,
body: plugin.body
}
);
}
};

interface CreateIndexParams {
client: Client;
plugins: PluginsContainer;
type: string;
tenant: string;
locale: string;
index: string;
onExists?: OnExists;
onError?: OnError;
}

export const createIndex = async (params: CreateIndexParams): Promise<void> => {
const { plugins, type, locale, onExists } = params;
const plugin = getLastAddedIndexPlugin<ElasticsearchIndexPlugin>({
container: plugins,
type,
locale
});

const exists = await indexExists(params);
if (exists) {
if (onExists) {
onExists();
}
return;
}

await indexCreate({
...params,
plugin
});
};
1 change: 1 addition & 0 deletions packages/api-elasticsearch/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./createIndex";
16 changes: 0 additions & 16 deletions packages/api-file-manager-ddb-es/package.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Client } from "@elastic/elasticsearch";
import { getLastAddedIndexPlugin } from "@webiny/api-elasticsearch";
import { createIndex } from "@webiny/api-elasticsearch";
import { FormElasticsearchIndexPlugin } from "~/plugins/FormElasticsearchIndexPlugin";
import { PluginsContainer } from "@webiny/plugins";
import WebinyError from "@webiny/error";
import { configurations } from "~/configurations";

interface CreateElasticsearchIndexParams {
Expand All @@ -13,43 +12,26 @@ interface CreateElasticsearchIndexParams {
}

export const createElasticsearchIndex = async (params: CreateElasticsearchIndexParams) => {
const { elasticsearch, plugins: container, locale, tenant } = params;

const plugin = getLastAddedIndexPlugin<FormElasticsearchIndexPlugin>({
container,
type: FormElasticsearchIndexPlugin.type,
locale
});
const { elasticsearch, plugins, locale, tenant } = params;

const { index } = configurations.es({
locale,
tenant
});

try {
const response = await elasticsearch.indices.exists({
index
});
if (response.body) {
return;
await createIndex({
client: elasticsearch,
index,
type: FormElasticsearchIndexPlugin.type,
tenant,
locale,
plugins,
onExists: () => {
console.log(`Elasticsearch index "${index}" for the Form Builder already exists.`);
},
onError: (ex: Error) => {
console.error(`Could not create the Form Builder Elasticsearch index "${index}".`);
return ex;
}

await elasticsearch.indices.create({
index,
body: plugin.body
});
} catch (ex) {
throw new WebinyError(
ex.message || "Could not create Elasticsearch index for the Form Builder.",
ex.code || "FB_ELASTICSEARCH_INDEX_ERROR",
{
error: ex,
type: FormElasticsearchIndexPlugin.type,
locale,
tenant,
index,
body: plugin.body
}
);
}
});
};
Original file line number Diff line number Diff line change
@@ -1,52 +1,40 @@
import WebinyError from "@webiny/error";
import { Client } from "@elastic/elasticsearch";
import { PluginsContainer } from "@webiny/plugins";
import { CmsEntryElasticsearchIndexPlugin } from "~/plugins/CmsEntryElasticsearchIndexPlugin";
import { getLastAddedIndexPlugin } from "@webiny/api-elasticsearch";
import { createIndex } from "@webiny/api-elasticsearch";
import { configurations } from "~/configurations";
import { CmsModel } from "@webiny/api-headless-cms/types";

export interface CreateElasticsearchIndexParams {
elasticsearch: Client;
client: Client;
plugins: PluginsContainer;
model: CmsModel;
}

export const createElasticsearchIndex = async (params: CreateElasticsearchIndexParams) => {
const { elasticsearch, plugins: container, model } = params;

const plugin = getLastAddedIndexPlugin<CmsEntryElasticsearchIndexPlugin>({
container,
type: CmsEntryElasticsearchIndexPlugin.type,
locale: model.locale
});
const { client, plugins, model } = params;

const { index } = configurations.es({
model
});

try {
const response = await elasticsearch.indices.exists({
index
});
if (response.body) {
return;
await createIndex({
client,
index,
type: CmsEntryElasticsearchIndexPlugin.type,
tenant: model.tenant,
locale: model.locale,
plugins,
onExists: () => {
console.log(
`Elasticsearch index "${index}" for the CMS model "${model.name}" already exists.`
);
},
onError: ex => {
console.error(
`Could not create Elasticsearch index "${index}" for the CMS model "${model.name}".`
);
return ex;
}
await elasticsearch.indices.create({
index,
body: plugin.body
});
} catch (ex) {
throw new WebinyError(
ex.message || "Could not create Elasticsearch index for the Headless CMS model.",
ex.code || "CMS_ELASTICSEARCH_INDEX_ERROR",
{
error: ex,
type: CmsEntryElasticsearchIndexPlugin.type,
locale: model.locale,
tenant: model.tenant,
body: plugin.body
}
);
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import { CmsModel } from "@webiny/api-headless-cms/types";
import { configurations } from "~/configurations";

interface DeleteElasticsearchIndexParams {
elasticsearch: Client;
client: Client;
model: CmsModel;
}

export const deleteElasticsearchIndex = async (
params: DeleteElasticsearchIndexParams
): Promise<void> => {
const { elasticsearch, model } = params;
const { client, model } = params;

const { index } = configurations.es({
model
});
const { body: exists } = await elasticsearch.indices.exists({
const { body: exists } = await client.indices.exists({
index
});
if (!exists) {
return;
}

try {
await elasticsearch.indices.delete({
await client.indices.delete({
index,
ignore_unavailable: true
});
Expand Down
15 changes: 11 additions & 4 deletions packages/api-headless-cms-ddb-es/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { createFilterPlugins } from "~/operations/entry/elasticsearch/filtering/plugins";
import { CmsEntryFilterPlugin } from "~/plugins/CmsEntryFilterPlugin";
import { StorageOperationsCmsModelPlugin } from "@webiny/api-headless-cms";
import { createElasticsearchIndexesOnLocaleAfterCreate } from "~/operations/system/indexes";

export * from "./plugins";

Expand Down Expand Up @@ -170,30 +171,36 @@ export const createStorageOperations: StorageOperationsFactory = params => {
* We need to create indexes on before model create and on clone (create from).
* Other apps create indexes on locale creation.
*/
await createElasticsearchIndexesOnLocaleAfterCreate({
context,
client: elasticsearch,
plugins
});

context.cms.onModelBeforeCreate.subscribe(async ({ model }) => {
await createElasticsearchIndex({
elasticsearch,
client: elasticsearch,
model,
plugins
});
});
context.cms.onModelBeforeCreateFrom.subscribe(async ({ model }) => {
await createElasticsearchIndex({
elasticsearch,
client: elasticsearch,
model,
plugins
});
});
context.cms.onModelAfterDelete.subscribe(async ({ model }) => {
await deleteElasticsearchIndex({
elasticsearch,
client: elasticsearch,
model
});
});

context.cms.onModelInitialize.subscribe(async ({ model }) => {
await createElasticsearchIndex({
elasticsearch,
client: elasticsearch,
model,
plugins
});
Expand Down

0 comments on commit ed3ef6a

Please sign in to comment.