2020-11-05 18:58:23 +02:00
|
|
|
import BaseModel, { ModelType } from '../../../BaseModel';
|
|
|
|
import { Request } from '../Api';
|
|
|
|
import defaultLoadOptions from '../utils/defaultLoadOptions';
|
|
|
|
import { ErrorBadRequest, ErrorMethodNotAllowed } from '../utils/errors';
|
|
|
|
import requestFields from '../utils/requestFields';
|
2020-11-11 13:52:47 +02:00
|
|
|
import collectionToPaginatedResults from '../utils/collectionToPaginatedResults';
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseItem from '../../../models/BaseItem';
|
2023-12-13 21:24:58 +02:00
|
|
|
import { NoteEntity } from '../../database/types';
|
2024-01-05 16:15:47 +02:00
|
|
|
import SearchEngineUtils, { NotesForQueryOptions } from '../../search/SearchEngineUtils';
|
2020-11-05 18:58:23 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export default async function(request: Request) {
|
2020-11-05 18:58:23 +02:00
|
|
|
if (request.method !== 'GET') throw new ErrorMethodNotAllowed();
|
|
|
|
|
|
|
|
const query = request.query.query;
|
|
|
|
if (!query) throw new ErrorBadRequest('Missing "query" parameter');
|
|
|
|
|
|
|
|
const modelType = request.query.type ? BaseModel.modelNameToType(request.query.type) : BaseModel.TYPE_NOTE;
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
let results: NoteEntity[] = [];
|
2020-11-05 18:58:23 +02:00
|
|
|
|
|
|
|
if (modelType !== BaseItem.TYPE_NOTE) {
|
|
|
|
const ModelClass = BaseItem.getClassByItemType(modelType);
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
const options: any = {};
|
2020-11-05 18:58:23 +02:00
|
|
|
const fields = requestFields(request, modelType);
|
|
|
|
if (fields.length) options.fields = fields;
|
|
|
|
const sqlQueryPart = query.replace(/\*/g, '%');
|
|
|
|
options.where = 'title LIKE ?';
|
|
|
|
options.whereParams = [sqlQueryPart];
|
|
|
|
options.caseInsensitive = true;
|
|
|
|
results = await ModelClass.all(options);
|
|
|
|
} else {
|
2023-11-19 13:44:34 +02:00
|
|
|
const options: NotesForQueryOptions = {
|
|
|
|
...defaultLoadOptions(request, ModelType.Note),
|
|
|
|
appendWildCards: true,
|
|
|
|
};
|
2023-12-13 21:24:58 +02:00
|
|
|
results = (await SearchEngineUtils.notesForQuery(query, false, options)).notes;
|
2020-11-05 18:58:23 +02:00
|
|
|
}
|
|
|
|
|
2024-04-27 10:23:09 +02:00
|
|
|
// 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 });
|
2020-11-05 18:58:23 +02:00
|
|
|
}
|