1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/ReactNativeClient/lib/services/SearchEngineUtils.js

32 lines
874 B
JavaScript

const SearchEngine = require('lib/services/SearchEngine');
const Note = require('lib/models/Note');
class SearchEngineUtils {
static async notesForQuery(query) {
const results = await SearchEngine.instance().search(query);
const noteIds = results.map(n => n.id);
const previewOptions = {
order: [],
fields: Note.previewFields(),
conditions: ['id IN ("' + noteIds.join('","') + '")'],
}
const notes = await Note.previews(null, previewOptions);
// By default, the notes will be returned in reverse order
// or maybe random order so sort them here in the correct order
// (search engine returns the results in order of relevance).
const sortedNotes = [];
for (let i = 0; i < notes.length; i++) {
const idx = noteIds.indexOf(notes[i].id);
sortedNotes[idx] = notes[i];
}
return sortedNotes;
}
}
module.exports = SearchEngineUtils;