diff --git a/.changeset/short-numbers-carry.md b/.changeset/short-numbers-carry.md new file mode 100644 index 0000000000..0b2fbe80ae --- /dev/null +++ b/.changeset/short-numbers-carry.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-backend-node': patch +--- + +Fixed bug preventing searches with filter values containing `:` from returning results. diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts index b09777217a..22c775da8e 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts @@ -339,6 +339,46 @@ describe('LunrSearchEngine', () => { }); }); + it('should perform search query and return search results on match with filters that include a : character', async () => { + const mockDocuments = [ + { + title: 'testTitle', + text: 'testText', + location: 'test:location', + }, + { + title: 'testTitle', + text: 'testText', + location: 'test:location2', + }, + ]; + + // Mock indexing of 2 documents + testLunrSearchEngine.index('test-index', mockDocuments); + + // Perform search query + const mockedSearchResult = await testLunrSearchEngine.query({ + term: 'testTitle', + filters: { location: 'test:location2' }, + pageCursor: '', + }); + + // Should return 1 of 2 results as we are + // 1. Mocking the indexing of 2 documents + // 2. Matching on the location field with the filter { location: 'test:location2' } + expect(mockedSearchResult).toMatchObject({ + results: [ + { + document: { + title: 'testTitle', + text: 'testText', + location: 'test:location2', + }, + }, + ], + }); + }); + it('should perform search query and return search results on match, scoped to specific index', async () => { const mockDocuments = [ { diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts index f2ace54ed2..a1c12f019d 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts @@ -57,6 +57,9 @@ export class LunrSearchEngine implements SearchEngine { .map(([field, value]) => { // Require that the given field has the given value (with +). if (['string', 'number', 'boolean'].includes(typeof value)) { + if (typeof value === 'string') { + return ` +${field}:${value.replace(':', '\\:')}`; + } return ` +${field}:${value}`; }