mirror of
https://github.com/zadam/trilium.git
synced 2025-11-13 08:45:50 +01:00
basic search tests
This commit is contained in:
@@ -17,6 +17,7 @@ class Attribute {
|
||||
/** @param {boolean} */
|
||||
this.isInheritable = !!row.isInheritable;
|
||||
|
||||
this.noteCache.attributes[this.attributeId] = this;
|
||||
this.noteCache.notes[this.noteId].ownedAttributes.push(this);
|
||||
|
||||
const key = `${this.type}-${this.name}`;
|
||||
|
||||
@@ -30,6 +30,7 @@ class Branch {
|
||||
|
||||
parentNote.children.push(childNote);
|
||||
|
||||
this.noteCache.branches[this.branchId] = this;
|
||||
this.noteCache.childParentToBranch[`${this.noteId}-${this.parentNoteId}`] = this;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ class Note {
|
||||
/** @param {string|null} */
|
||||
this.flatTextCache = null;
|
||||
|
||||
this.noteCache.notes[this.noteId] = this;
|
||||
|
||||
if (protectedSessionService.isProtectedSessionAvailable()) {
|
||||
this.decrypt();
|
||||
}
|
||||
|
||||
@@ -6,16 +6,20 @@ const Attribute = require('./entities/attribute');
|
||||
|
||||
class NoteCache {
|
||||
constructor() {
|
||||
this.reset();
|
||||
}
|
||||
|
||||
reset() {
|
||||
/** @type {Object.<String, Note>} */
|
||||
this.notes = null;
|
||||
this.notes = [];
|
||||
/** @type {Object.<String, Branch>} */
|
||||
this.branches = null;
|
||||
this.branches = [];
|
||||
/** @type {Object.<String, Branch>} */
|
||||
this.childParentToBranch = {};
|
||||
/** @type {Object.<String, Attribute>} */
|
||||
this.attributes = null;
|
||||
this.attributes = [];
|
||||
/** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes them */
|
||||
this.attributeIndex = null;
|
||||
this.attributeIndex = {};
|
||||
|
||||
this.loaded = false;
|
||||
this.loadedResolve = null;
|
||||
|
||||
@@ -11,34 +11,20 @@ const Attribute = require('./entities/attribute');
|
||||
async function load() {
|
||||
await sqlInit.dbReady;
|
||||
|
||||
noteCache.notes = await getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`,
|
||||
row => new Note(noteCache, row));
|
||||
noteCache.reset();
|
||||
|
||||
noteCache.branches = await getMappedRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`,
|
||||
row => new Branch(noteCache, row));
|
||||
(await sql.getRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`, []))
|
||||
.map(row => new Note(noteCache, row));
|
||||
|
||||
noteCache.attributeIndex = [];
|
||||
(await sql.getRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`, []))
|
||||
.map(row => new Branch(noteCache, row));
|
||||
|
||||
noteCache.attributes = await getMappedRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`,
|
||||
row => new Attribute(noteCache, row));
|
||||
(await sql.getRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`, [])).map(row => new Attribute(noteCache, row));
|
||||
|
||||
noteCache.loaded = true;
|
||||
noteCache.loadedResolve();
|
||||
}
|
||||
|
||||
async function getMappedRows(query, cb) {
|
||||
const map = {};
|
||||
const results = await sql.getRows(query, []);
|
||||
|
||||
for (const row of results) {
|
||||
const keys = Object.keys(row);
|
||||
|
||||
map[row[keys[0]]] = cb(row);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED], async ({entityName, entity}) => {
|
||||
// note that entity can also be just POJO without methods if coming from sync
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
class AndExp {
|
||||
const Expression = require('./expression');
|
||||
|
||||
class AndExp extends Expression{
|
||||
static of(subExpressions) {
|
||||
subExpressions = subExpressions.filter(exp => !!exp);
|
||||
|
||||
@@ -12,6 +14,7 @@ class AndExp {
|
||||
}
|
||||
|
||||
constructor(subExpressions) {
|
||||
super();
|
||||
this.subExpressions = subExpressions;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
const NoteSet = require('../note_set');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
const Expression = require('./expression');
|
||||
|
||||
class AttributeExistsExp {
|
||||
class AttributeExistsExp extends Expression {
|
||||
constructor(attributeType, attributeName, prefixMatch) {
|
||||
super();
|
||||
|
||||
this.attributeType = attributeType;
|
||||
this.attributeName = attributeName;
|
||||
this.prefixMatch = prefixMatch;
|
||||
|
||||
11
src/services/search/expressions/expression.js
Normal file
11
src/services/search/expressions/expression.js
Normal file
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
class Expression {
|
||||
/**
|
||||
* @param {NoteSet} noteSet
|
||||
* @param {object} searchContext
|
||||
*/
|
||||
execute(noteSet, searchContext) {}
|
||||
}
|
||||
|
||||
module.exports = Expression;
|
||||
@@ -1,10 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const Expression = require('./expression');
|
||||
const NoteSet = require('../note_set');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
|
||||
class FieldComparisonExp {
|
||||
class FieldComparisonExp extends Expression {
|
||||
constructor(attributeType, attributeName, comparator) {
|
||||
super();
|
||||
|
||||
this.attributeType = attributeType;
|
||||
this.attributeName = attributeName;
|
||||
this.comparator = comparator;
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
class NotExp {
|
||||
const Expression = require('./expression');
|
||||
|
||||
class NotExp extends Expression {
|
||||
constructor(subExpression) {
|
||||
super();
|
||||
|
||||
this.subExpression = subExpression;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const Expression = require('./expression');
|
||||
const NoteSet = require('../note_set');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
|
||||
class NoteCacheFulltextExp {
|
||||
class NoteCacheFulltextExp extends Expression {
|
||||
constructor(tokens) {
|
||||
super();
|
||||
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const Expression = require('./expression');
|
||||
const NoteSet = require('../note_set');
|
||||
const noteCache = require('../../note_cache/note_cache');
|
||||
|
||||
class NoteContentFulltextExp {
|
||||
class NoteContentFulltextExp extends Expression {
|
||||
constructor(tokens) {
|
||||
super();
|
||||
|
||||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
const Expression = require('./expression');
|
||||
const NoteSet = require('../note_set');
|
||||
|
||||
class OrExp {
|
||||
class OrExp extends Expression {
|
||||
static of(subExpressions) {
|
||||
subExpressions = subExpressions.filter(exp => !!exp);
|
||||
|
||||
@@ -15,6 +16,8 @@ class OrExp {
|
||||
}
|
||||
|
||||
constructor(subExpressions) {
|
||||
super();
|
||||
|
||||
this.subExpressions = subExpressions;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
function lexer(str) {
|
||||
str = str.toLowerCase();
|
||||
|
||||
const fulltextTokens = [];
|
||||
const expressionTokens = [];
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ const noteCacheService = require('../note_cache/note_cache_service');
|
||||
const hoistedNoteService = require('../hoisted_note');
|
||||
const utils = require('../utils');
|
||||
|
||||
/**
|
||||
* @param {Expression} expression
|
||||
* @return {Promise<SearchResult[]>}
|
||||
*/
|
||||
async function findNotesWithExpression(expression) {
|
||||
const hoistedNote = noteCache.notes[hoistedNoteService.getHoistedNoteId()];
|
||||
const allNotes = (hoistedNote && hoistedNote.noteId !== 'root')
|
||||
@@ -56,6 +60,21 @@ function parseQueryToExpression(query, parsingContext) {
|
||||
return expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} query
|
||||
* @param {ParsingContext} parsingContext
|
||||
* @return {Promise<SearchResult[]>}
|
||||
*/
|
||||
async function findNotesWithQuery(query, parsingContext) {
|
||||
const expression = parseQueryToExpression(query, parsingContext);
|
||||
|
||||
if (!expression) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return await findNotesWithExpression(expression);
|
||||
}
|
||||
|
||||
async function searchNotesForAutocomplete(query) {
|
||||
if (!query.trim().length) {
|
||||
return [];
|
||||
@@ -66,13 +85,7 @@ async function searchNotesForAutocomplete(query) {
|
||||
fuzzyAttributeSearch: true
|
||||
});
|
||||
|
||||
const expression = parseQueryToExpression(query, parsingContext);
|
||||
|
||||
if (!expression) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let searchResults = await findNotesWithExpression(expression);
|
||||
let searchResults = findNotesWithQuery(query, parsingContext);
|
||||
|
||||
searchResults = searchResults.slice(0, 200);
|
||||
|
||||
@@ -141,5 +154,6 @@ function formatAttribute(attr) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
searchNotesForAutocomplete
|
||||
searchNotesForAutocomplete,
|
||||
findNotesWithQuery
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user