1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixes #10088: Search results from API change when fields param is used

This commit is contained in:
Laurent Cozic 2024-04-27 09:23:09 +01:00
parent 8bdec4c2b4
commit 10978781cd
2 changed files with 31 additions and 17 deletions

View File

@ -37,5 +37,8 @@ export default async function(request: Request) {
results = (await SearchEngineUtils.notesForQuery(query, false, options)).notes;
}
return collectionToPaginatedResults(modelType, results, request);
// We do not sort the results because the search engine has already sorted them in order of
// relevance.
// https://github.com/laurent22/joplin/issues/10088
return collectionToPaginatedResults(modelType, results, request, { sort: false });
}

View File

@ -5,13 +5,22 @@ import { Request } from '../Api';
import requestPaginationOptions from '../utils/requestPaginationOptions';
import requestFields from './requestFields';
export interface Options {
sort?: boolean;
}
// Note that this is inefficient pagination as it requires all the items to
// have been loaded first, even if not all of them are returned.
//
// It's however convenient for smaller lists as it reduces the need for
// building complex SQL queries.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
export default function(itemType: ModelType, items: any[], request: Request): ModelFeedPage {
export default function(itemType: ModelType, items: any[], request: Request, options: Options = null): ModelFeedPage {
options = {
sort: true,
...options,
};
const fields = requestFields(request, itemType);
const pagination = requestPaginationOptions(request);
const startIndex = (pagination.page - 1) * pagination.limit;
@ -33,23 +42,25 @@ export default function(itemType: ModelType, items: any[], request: Request): Mo
return newItem;
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
sortedItems.sort((a: any, b: any) => {
let v1 = a && (sortBy in a) ? a[sortBy] : '';
let v2 = b && (sortBy in b) ? b[sortBy] : '';
if (caseInsensitive && typeof v1 === 'string') v1 = v1.toLowerCase();
if (caseInsensitive && typeof v2 === 'string') v2 = v2.toLowerCase();
if (options.sort) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
sortedItems.sort((a: any, b: any) => {
let v1 = a && (sortBy in a) ? a[sortBy] : '';
let v2 = b && (sortBy in b) ? b[sortBy] : '';
if (caseInsensitive && typeof v1 === 'string') v1 = v1.toLowerCase();
if (caseInsensitive && typeof v2 === 'string') v2 = v2.toLowerCase();
if (sortDir === PaginationOrderDir.ASC) {
if (v1 < v2) return -1;
if (v2 > v1) return +1;
} else {
if (v1 > v2) return -1;
if (v2 < v1) return +1;
}
if (sortDir === PaginationOrderDir.ASC) {
if (v1 < v2) return -1;
if (v2 > v1) return +1;
} else {
if (v1 > v2) return -1;
if (v2 < v1) return +1;
}
return 0;
});
return 0;
});
}
return {
items: sortedItems.slice(startIndex, startIndex + itemCount),