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

feat(elastic-search): improved default search #3284

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/elasticsearch-plugin/e2e/e2e-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Camera Lens is now the first result because name is more important. In most cases this is desired, but this test case is debatable... WDYT?

'SLR Camera',
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -913,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);
Copy link
Collaborator Author

@martijnvdbrug martijnvdbrug Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzzy matching returns multiple results now, but this test only cares about if T3 is disabled, so we should ignore the other results

});

// https://github.com/vendure-ecommerce/vendure/issues/295
Expand Down
12 changes: 8 additions & 4 deletions packages/elasticsearch-plugin/src/build-elastic-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
},
],
Expand Down Expand Up @@ -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'],
},
},
],
Expand Down Expand Up @@ -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'],
},
},
],
Expand Down Expand Up @@ -456,6 +459,7 @@ describe('buildElasticBody()', () => {
multi_match: {
query: 'test',
type: 'best_fields',
fuzziness: 'AUTO',
fields: ['productName^3', 'productVariantName^4', 'description^2', 'sku^5'],
},
},
Expand All @@ -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)",
},
});
});
Expand Down
7 changes: 4 additions & 3 deletions packages/elasticsearch-plugin/src/build-elastic-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function buildElasticBody(
{
multi_match: {
query: term,
fuzziness: 'AUTO',
type: searchConfig.multiMatchType,
fields: [
`productName^${searchConfig.boostFields.productName}`,
Expand Down Expand Up @@ -166,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;
Expand Down
4 changes: 2 additions & 2 deletions packages/elasticsearch-plugin/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
Loading