@vegan-friendly/strapi-plugin-elasticsearch 0.2.4-alpha.1 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vegan-friendly/strapi-plugin-elasticsearch",
3
- "version": "0.2.4-alpha.1",
3
+ "version": "0.2.4",
4
4
  "description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
5
5
  "homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
6
6
  "strapi": {
@@ -151,7 +151,7 @@ declare const _default: {
151
151
  strapi: any;
152
152
  }) => {
153
153
  rebuildIndex(): Promise<boolean>;
154
- indexCollection(collectionName: any, indexName?: null): Promise<boolean>;
154
+ indexCollection(collectionName: any, indexName?: string | null): Promise<boolean>;
155
155
  indexPendingData(): Promise<boolean>;
156
156
  };
157
157
  logIndexing: ({ strapi }: {
@@ -114,7 +114,8 @@ exports.default = ({ strapi }) => ({
114
114
  id: itemId,
115
115
  document: itemData,
116
116
  });
117
- await client.indices.refresh({ index: iName });
117
+ //indices.refresh is an expensive operation. and ES is doing this once a second anyway (see https://www.elastic.co/guide/en/elasticsearch/reference/8.17/indices-refresh.html)
118
+ // await client!.indices.refresh({ index: iName });
118
119
  }
119
120
  catch (err) {
120
121
  console.log('strapi-plugin-elasticsearch : Error encountered while indexing data to ElasticSearch.');
@@ -126,14 +127,13 @@ exports.default = ({ strapi }) => ({
126
127
  const pluginConfig = await strapi.config.get('plugin.elasticsearch');
127
128
  return await this.indexDataToSpecificIndex({ itemId, itemData }, pluginConfig.indexAliasName);
128
129
  },
129
- async removeItemFromIndex({ itemId }) {
130
- const pluginConfig = await strapi.config.get('plugin.elasticsearch');
130
+ async removeItemFromIndex({ indexName, itemId }) {
131
131
  try {
132
132
  await client.delete({
133
- index: pluginConfig.indexAliasName,
133
+ index: indexName,
134
134
  id: itemId,
135
135
  });
136
- await client.indices.refresh({ index: pluginConfig.indexAliasName });
136
+ await client.indices.refresh({ index: indexName });
137
137
  }
138
138
  catch (err) {
139
139
  if (err?.meta?.statusCode === 404)
@@ -47,7 +47,7 @@ declare const _default: {
47
47
  strapi: any;
48
48
  }) => {
49
49
  rebuildIndex(): Promise<boolean>;
50
- indexCollection(collectionName: any, indexName?: null): Promise<boolean>;
50
+ indexCollection(collectionName: any, indexName?: string | null): Promise<boolean>;
51
51
  indexPendingData(): Promise<boolean>;
52
52
  };
53
53
  logIndexing: ({ strapi }: {
@@ -2,7 +2,7 @@ declare const _default: ({ strapi }: {
2
2
  strapi: any;
3
3
  }) => {
4
4
  rebuildIndex(): Promise<boolean>;
5
- indexCollection(collectionName: any, indexName?: null): Promise<boolean>;
5
+ indexCollection(collectionName: any, indexName?: string | null): Promise<boolean>;
6
6
  indexPendingData(): Promise<boolean>;
7
7
  };
8
8
  export default _default;
@@ -101,6 +101,7 @@ exports.default = ({ strapi }) => ({
101
101
  const logIndexingService = strapi.plugins['elasticsearch'].services.logIndexing;
102
102
  const esInterface = strapi.plugins['elasticsearch'].services.esInterface;
103
103
  const helper = strapi.plugins['elasticsearch'].services.helper;
104
+ const indexAlias = await strapi.config.get('plugin.elasticsearch').indexAliasName;
104
105
  const recs = await scheduleIndexingService.getItemsPendingToBeIndexed();
105
106
  const fullSiteIndexing = recs.filter((r) => r.full_site_indexing === true).length > 0;
106
107
  if (fullSiteIndexing) {
@@ -137,7 +138,7 @@ exports.default = ({ strapi }) => ({
137
138
  collectionName: col,
138
139
  itemId: recs[r].item_id,
139
140
  });
140
- await esInterface.removeItemFromIndex({ itemId: indexItemId });
141
+ await esInterface.removeItemFromIndex({ indexName: indexAlias, itemId: indexItemId });
141
142
  await scheduleIndexingService.markIndexingTaskComplete(recs[r].id);
142
143
  }
143
144
  } //index the entire collection
@@ -22,7 +22,7 @@ exports.default = ({ strapi }) => {
22
22
  throw new Error(`Virtual collection not found: ${collectionName}`);
23
23
  }
24
24
  try {
25
- const results = await collection.extractById([itemId]);
25
+ const results = await collection.extractByIds([itemId]);
26
26
  if (!results || !Array.isArray(results) || results.length === 0) {
27
27
  strapi.log.warn(`No data extracted for ${collectionName} with ID ${itemId}`);
28
28
  return null;
@@ -113,15 +113,25 @@ exports.default = ({ strapi }) => {
113
113
  const { model, result } = event;
114
114
  const registry = getRegistryService();
115
115
  // Find virtual collections that should be triggered by this model
116
- const affectedCollections = registry.findTriggersByCollection(model);
116
+ const affectedCollections = registry.findTriggersByCollection(model.uid);
117
117
  for (const collection of affectedCollections) {
118
118
  // Find the specific trigger for this collection
119
- const trigger = collection.triggers.find((t) => t.collection === model);
120
- if (trigger && trigger.getIdsToReindex) {
121
- // Get IDs that need to be reindexed
122
- const idsToReindex = await trigger.getIdsToReindex(result);
123
- // Reindex each item
124
- for (const id of idsToReindex) {
119
+ const trigger = collection.triggers.find((t) => t.collection === model.uid);
120
+ const triggerIsOnIndexCollection = model.uid === collection.collectionName;
121
+ if (trigger?.getIdsToReindex == null) {
122
+ strapi.log.error(`Trigger for ${collection.collectionName} (triggered by ${model.uid}) does not have getIdsToReindex function.`);
123
+ return;
124
+ }
125
+ // Get IDs that need to be reindexed
126
+ const idsToReindex = await trigger.getIdsToReindex(result);
127
+ // Reindex each item
128
+ for (const id of idsToReindex) {
129
+ const isDelete = event.action?.toLowerCase()?.includes('delete') && triggerIsOnIndexCollection && id === result.id;
130
+ if (isDelete) {
131
+ //delete the item from the index, if the item being delete is the one being reindexed
132
+ await this.deleteItem(collection.collectionName, id);
133
+ }
134
+ else {
125
135
  await this.indexItem(collection.collectionName, id);
126
136
  }
127
137
  }
@@ -132,13 +142,16 @@ exports.default = ({ strapi }) => {
132
142
  */
133
143
  async deleteItem(collectionName, itemId) {
134
144
  const registry = getRegistryService();
145
+ const helper = getHelperService();
135
146
  const collection = registry.get(collectionName);
136
147
  if (!collection) {
137
148
  throw new Error(`Virtual collection not found: ${collectionName}`);
138
149
  }
139
150
  try {
140
151
  const esInterface = getElasticsearchService();
141
- await esInterface.removeItemFromIndex({ itemId });
152
+ const indexItemId = helper.getIndexItemId({ collectionName, itemId });
153
+ const indexName = collection.indexAlias || (await helper.getCurrentIndexName());
154
+ await esInterface.removeItemFromIndex({ indexName, itemId: indexItemId });
142
155
  strapi.log.debug(`Deleted indexed item: ${collectionName}:${itemId}`);
143
156
  return true;
144
157
  }
@@ -61,6 +61,7 @@ export interface EsInterfaceService {
61
61
  * @returns A promise that resolves when the item is removed.
62
62
  */
63
63
  removeItemFromIndex(data: {
64
+ indexName: string;
64
65
  itemId: string;
65
66
  }): Promise<any>;
66
67
  /**
@@ -21,8 +21,8 @@ export interface HelperService {
21
21
  collectionName: string;
22
22
  itemId: number;
23
23
  }): string;
24
- getCurrentIndexName(indexAlias?: string): Promise<string>;
25
- getIncrementedIndexName(indexPrefix: string): Promise<string>;
24
+ getCurrentIndexName(indexPrefix?: string): Promise<string>;
25
+ getIncrementedIndexName(indexPrefix?: string): Promise<string>;
26
26
  deleteOldIndices(indexAlias?: string): Promise<string[]>;
27
27
  modifySubfieldsConfigForExtractor(collectionConfig: object): object;
28
28
  extractDataToIndex(args: {
@@ -13,7 +13,7 @@ export type VirtualCollectionConfig<T extends StrapiEntity> = {
13
13
  indexAlias?: string;
14
14
  collectionName: string;
15
15
  extractData: (page: number, pageSize?: number) => Promise<T[]>;
16
- extractById: (ids: number[]) => Promise<T[]>;
16
+ extractByIds: (ids: number[]) => Promise<T[]>;
17
17
  triggers: Array<{
18
18
  collection: string;
19
19
  getIdsToReindex: (result: any) => Promise<number[]>;
@@ -70,5 +70,5 @@ export interface VirtualCollectionsIndexerService {
70
70
  * @param itemId - The id of the item to be deleted.
71
71
  * @returns A promise that resolves to a boolean indicating success.
72
72
  */
73
- deleteItem(collectionName: string, itemId: string): Promise<boolean>;
73
+ deleteItem(collectionName: string, itemId: number): Promise<boolean>;
74
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vegan-friendly/strapi-plugin-elasticsearch",
3
- "version": "0.2.4-alpha.1",
3
+ "version": "0.2.5",
4
4
  "description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
5
5
  "homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
6
6
  "strapi": {