fix(elasticSearch): do not define pageCursor on last page
Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-search-backend-module-elasticsearch': patch
|
||||
---
|
||||
|
||||
Fix issue where `nextPageCursor` is defined on the last page of results
|
||||
+44
@@ -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',
|
||||
|
||||
+1
-1
@@ -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 })
|
||||
|
||||
Reference in New Issue
Block a user