Merge pull request #6098 from jessebye/lunr-escape-colon
Escape colons in Lunr search query filters
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search-backend-node': patch
|
||||
---
|
||||
|
||||
Fixed bug preventing searches with filter values containing `:` from returning results.
|
||||
@@ -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 = [
|
||||
{
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user