From 190b5927cbcc3c0fcd4f8bfc0a22ff8993fa7c27 Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 20 Dec 2024 15:40:21 +0100 Subject: [PATCH 1/6] feat(elastic-search): improved default search --- package.json | 3 ++- packages/elasticsearch-plugin/src/build-elastic-body.ts | 1 + packages/elasticsearch-plugin/src/options.ts | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f474eeee83..e92593d650 100644 --- a/package.json +++ b/package.json @@ -104,5 +104,6 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-jsdoc": "^45.0.0", "eslint-plugin-prefer-arrow": "^1.2.3" - } + }, + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } diff --git a/packages/elasticsearch-plugin/src/build-elastic-body.ts b/packages/elasticsearch-plugin/src/build-elastic-body.ts index 3e267634c1..10823222ba 100644 --- a/packages/elasticsearch-plugin/src/build-elastic-body.ts +++ b/packages/elasticsearch-plugin/src/build-elastic-body.ts @@ -41,6 +41,7 @@ export function buildElasticBody( { multi_match: { query: term, + fuzziness: "AUTO", type: searchConfig.multiMatchType, fields: [ `productName^${searchConfig.boostFields.productName}`, diff --git a/packages/elasticsearch-plugin/src/options.ts b/packages/elasticsearch-plugin/src/options.ts index 664e42387a..f37bb65dc1 100644 --- a/packages/elasticsearch-plugin/src/options.ts +++ b/packages/elasticsearch-plugin/src/options.ts @@ -728,8 +728,8 @@ export const defaultOptions: ElasticsearchRuntimeOptions = { totalItemsMaxSize: 10000, multiMatchType: 'best_fields', boostFields: { - productName: 1, - productVariantName: 1, + productName: 5, + productVariantName: 5, description: 1, sku: 1, }, From 5fb66d6d7b05d650567ed7a96c9ee81da16a7ddd Mon Sep 17 00:00:00 2001 From: Martijn Date: Fri, 20 Dec 2024 15:44:11 +0100 Subject: [PATCH 2/6] feat(elastic-search): revert package lock --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index e92593d650..f474eeee83 100644 --- a/package.json +++ b/package.json @@ -104,6 +104,5 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-jsdoc": "^45.0.0", "eslint-plugin-prefer-arrow": "^1.2.3" - }, - "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" + } } From 863b154fc44d36a46edec5c08c551bea1c76a688 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sat, 21 Dec 2024 11:55:19 +0100 Subject: [PATCH 3/6] feat(vendure): test fix --- .../src/build-elastic-body.spec.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/elasticsearch-plugin/src/build-elastic-body.spec.ts b/packages/elasticsearch-plugin/src/build-elastic-body.spec.ts index e52d30887c..b9815a0f81 100644 --- a/packages/elasticsearch-plugin/src/build-elastic-body.spec.ts +++ b/packages/elasticsearch-plugin/src/build-elastic-body.spec.ts @@ -21,7 +21,8 @@ describe('buildElasticBody()', () => { multi_match: { query: 'test', type: 'best_fields', - fields: ['productName^1', 'productVariantName^1', 'description^1', 'sku^1'], + fuzziness: 'AUTO', + fields: ['productName^5', 'productVariantName^5', 'description^1', 'sku^1'], }, }, ], @@ -389,7 +390,8 @@ describe('buildElasticBody()', () => { multi_match: { query: 'test', type: 'best_fields', - fields: ['productName^1', 'productVariantName^1', 'description^1', 'sku^1'], + fuzziness: 'AUTO', + fields: ['productName^5', 'productVariantName^5', 'description^1', 'sku^1'], }, }, ], @@ -427,7 +429,8 @@ describe('buildElasticBody()', () => { multi_match: { query: 'test', type: 'phrase', - fields: ['productName^1', 'productVariantName^1', 'description^1', 'sku^1'], + fuzziness: 'AUTO', + fields: ['productName^5', 'productVariantName^5', 'description^1', 'sku^1'], }, }, ], @@ -456,6 +459,7 @@ describe('buildElasticBody()', () => { multi_match: { query: 'test', type: 'best_fields', + fuzziness: 'AUTO', fields: ['productName^3', 'productVariantName^4', 'description^2', 'sku^5'], }, }, @@ -482,7 +486,7 @@ describe('buildElasticBody()', () => { const result = buildElasticBody({ term: 'test' }, config, CHANNEL_ID, LanguageCode.en); expect(result.script_fields).toEqual({ test: { - script: 'doc[\'property\'].dummyScript(test)', + script: "doc['property'].dummyScript(test)", }, }); }); From 5f27369dc68dcaa728bdaf0847989f779dd574f1 Mon Sep 17 00:00:00 2001 From: Martijn Date: Sat, 21 Dec 2024 12:23:56 +0100 Subject: [PATCH 4/6] feat(elastic-search): Equal weights for e2e --- .../e2e/elasticsearch-plugin.e2e-spec.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts index 24502cc6a8..9ee4b71f61 100644 --- a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts +++ b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts @@ -155,6 +155,12 @@ describe('Elasticsearch plugin', () => { }, }, }, + boostFields: { + description: 1, + productName: 1, + productVariantName: 1, + sku: 1, + }, mapSort: (sort, input) => { const priority = (input.sort as any)?.priority; if (priority) { From 9691675c7c3c7491e589436e294b9f2b88a8116e Mon Sep 17 00:00:00 2001 From: Martijn Date: Tue, 24 Dec 2024 11:25:04 +0100 Subject: [PATCH 5/6] feat(elastic-search): Fixed e2e with multiple results because of fuzzy matching --- .../e2e/elasticsearch-plugin.e2e-spec.ts | 11 ++--------- .../elasticsearch-plugin/src/build-elastic-body.ts | 8 ++++---- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts index 9ee4b71f61..12dab2ab99 100644 --- a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts +++ b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts @@ -155,12 +155,6 @@ describe('Elasticsearch plugin', () => { }, }, }, - boostFields: { - description: 1, - productName: 1, - productVariantName: 1, - sku: 1, - }, mapSort: (sort, input) => { const priority = (input.sort as any)?.priority; if (priority) { @@ -919,9 +913,8 @@ describe('Elasticsearch plugin', () => { groupByProduct: true, term: 'gaming', }); - expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([ - { productId: 'T_3', enabled: false }, - ]); + const t3 = result.search.items.find(i => i.productId === 'T_3'); + expect(t3?.enabled).toEqual(false); }); // https://github.com/vendure-ecommerce/vendure/issues/295 diff --git a/packages/elasticsearch-plugin/src/build-elastic-body.ts b/packages/elasticsearch-plugin/src/build-elastic-body.ts index 10823222ba..96bf132135 100644 --- a/packages/elasticsearch-plugin/src/build-elastic-body.ts +++ b/packages/elasticsearch-plugin/src/build-elastic-body.ts @@ -41,7 +41,7 @@ export function buildElasticBody( { multi_match: { query: term, - fuzziness: "AUTO", + fuzziness: 'AUTO', type: searchConfig.multiMatchType, fields: [ `productName^${searchConfig.boostFields.productName}`, @@ -167,13 +167,13 @@ function createScriptFields( for (const name of fields) { const scriptField = scriptFields[name]; if (scriptField.context === 'product' && groupByProduct === true) { - (result )[name] = scriptField.scriptFn(input); + result[name] = scriptField.scriptFn(input); } if (scriptField.context === 'variant' && groupByProduct === false) { - (result )[name] = scriptField.scriptFn(input); + result[name] = scriptField.scriptFn(input); } if (scriptField.context === 'both' || scriptField.context === undefined) { - (result )[name] = scriptField.scriptFn(input); + result[name] = scriptField.scriptFn(input); } } return result; From b8b3600bfcbbae7210bd6d3f087251e5a66abbd1 Mon Sep 17 00:00:00 2001 From: Martijn Date: Tue, 24 Dec 2024 12:08:36 +0100 Subject: [PATCH 6/6] feat(elastic-search): Order of serachterm test --- packages/elasticsearch-plugin/e2e/e2e-helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/elasticsearch-plugin/e2e/e2e-helpers.ts b/packages/elasticsearch-plugin/e2e/e2e-helpers.ts index c2cca7dfa3..69b45f24a5 100644 --- a/packages/elasticsearch-plugin/e2e/e2e-helpers.ts +++ b/packages/elasticsearch-plugin/e2e/e2e-helpers.ts @@ -63,8 +63,8 @@ export async function testMatchSearchTerm(client: SimpleGraphQLClient) { }, ); expect(result.search.items.map(i => i.productName)).toEqual([ - 'Instant Camera', 'Camera Lens', + 'Instant Camera', 'SLR Camera', ]); }