From 4b2a9b55092fb071df4c5a918dc0d6125eb65e42 Mon Sep 17 00:00:00 2001 From: rdeepc <12953177+rdeepc@users.noreply.github.com> Date: Mon, 22 May 2023 21:45:52 -0400 Subject: [PATCH 1/3] Updated fields search for postgres to support array item Signed-off-by: rdeepc <12953177+rdeepc@users.noreply.github.com> --- .../src/database/DatabaseDocumentStore.test.ts | 16 ++++++++++++++++ .../src/database/DatabaseDocumentStore.ts | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts index 2f99a02521..0804f643ab 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts @@ -453,6 +453,12 @@ describe('DatabaseDocumentStore', () => { text: 'Around the world', location: 'LOCATION-1', }, + { + title: 'Sed ut perspiciatis', + text: 'Hello World', + myField: ['that', 'not'], + location: 'LOCATION-1', + } as unknown as IndexableDocument, ]); await store.completeInsert(tx, 'my-type'); }); @@ -488,6 +494,16 @@ describe('DatabaseDocumentStore', () => { rank: expect.any(Number), type: 'my-type', }, + { + document: { + location: 'LOCATION-1', + text: 'Hello World', + title: 'Sed ut perspiciatis', + myField: ['that', 'not'], + }, + rank: expect.any(Number), + type: 'my-type', + }, ]); }, ); diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts index 023992a335..719cc1c755 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts @@ -171,9 +171,13 @@ export class DatabaseDocumentStore implements DatabaseStore { Object.keys(fields).forEach(key => { const value = fields[key]; const valueArray = Array.isArray(value) ? value : [value]; - const valueCompare = valueArray + const fieldValueCompare = valueArray .map(v => ({ [key]: v })) .map(v => JSON.stringify(v)); + const arrayValueCompare = valueArray + .map(v => ({ [key]: [v] })) + .map(v => JSON.stringify(v)); + const valueCompare = [...fieldValueCompare, ...arrayValueCompare]; query.whereRaw( `(${valueCompare.map(() => 'document @> ?').join(' OR ')})`, valueCompare, From 3c09e8d3cb0cbe3dbf1c64e1b2265f829f54968d Mon Sep 17 00:00:00 2001 From: rdeepc <12953177+rdeepc@users.noreply.github.com> Date: Mon, 22 May 2023 21:55:40 -0400 Subject: [PATCH 2/3] Updated fields search for postgres to support array item Signed-off-by: rdeepc <12953177+rdeepc@users.noreply.github.com> --- .changeset/shaggy-terms-travel.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shaggy-terms-travel.md diff --git a/.changeset/shaggy-terms-travel.md b/.changeset/shaggy-terms-travel.md new file mode 100644 index 0000000000..a131608487 --- /dev/null +++ b/.changeset/shaggy-terms-travel.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-backend-module-pg': patch +--- + +Updated Postgres search query filter in DatabaseDocumentStore to support field value search in array. From 4a44b9f3d8f8c9b8bc350dc6be3a86327e968573 Mon Sep 17 00:00:00 2001 From: rdeepc <12953177+rdeepc@users.noreply.github.com> Date: Tue, 23 May 2023 20:48:29 -0400 Subject: [PATCH 3/3] Updated test with 3 items in array Signed-off-by: rdeepc <12953177+rdeepc@users.noreply.github.com> --- .../src/database/DatabaseDocumentStore.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts index 0804f643ab..c5dbb74da1 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts @@ -459,6 +459,12 @@ describe('DatabaseDocumentStore', () => { myField: ['that', 'not'], location: 'LOCATION-1', } as unknown as IndexableDocument, + { + title: 'Consectetur adipiscing', + text: 'Hello World', + myField: ['that', 'not', 'where'], + location: 'LOCATION-1', + } as unknown as IndexableDocument, ]); await store.completeInsert(tx, 'my-type'); }); @@ -504,6 +510,16 @@ describe('DatabaseDocumentStore', () => { rank: expect.any(Number), type: 'my-type', }, + { + document: { + location: 'LOCATION-1', + text: 'Hello World', + title: 'Consectetur adipiscing', + myField: ['that', 'not', 'where'], + }, + rank: expect.any(Number), + type: 'my-type', + }, ]); }, );