fix(elasticSearch): do not define pageCursor on last page

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2022-05-12 21:32:44 -04:00
parent b96380b114
commit 9eef9c9db4
3 changed files with 50 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-elasticsearch': patch
---
Fix issue where `nextPageCursor` is defined on the last page of results
@@ -438,6 +438,50 @@ describe('ElasticSearchSearchEngine', () => {
});
});
it('should perform search query with less results than one page', async () => {
mock.clear({
method: 'POST',
path: '/*__search/_search',
});
mock.add(
{
method: 'POST',
path: '/*__search/_search',
},
() => {
return {
hits: {
total: { value: 20, relation: 'eq' },
hits: Array(20)
.fill(null)
.map((_, i) => ({
_index: 'mytype-index__',
_source: {
value: `${i}`,
},
})),
},
};
},
);
const mockedSearchResult = await testSearchEngine.query({
term: 'testTerm',
filters: {},
});
expect(mockedSearchResult).toMatchObject({
results: expect.arrayContaining(
Array(20)
.fill(null)
.map((_, i) => ({
type: 'mytype',
document: { value: `${i}` },
})),
),
});
});
it('should perform search query with more results than one page', async () => {
mock.clear({
method: 'POST',
@@ -268,7 +268,7 @@ export class ElasticSearchSearchEngine implements SearchEngine {
body: elasticSearchQuery,
});
const { page } = decodePageCursor(query.pageCursor);
const hasNextPage = result.body.hits.total.value > page * pageSize;
const hasNextPage = result.body.hits.total.value > (page + 1) * pageSize;
const hasPreviousPage = page > 0;
const nextPageCursor = hasNextPage
? encodePageCursor({ page: page + 1 })