mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	basic search tests
This commit is contained in:
		| @@ -18,8 +18,8 @@ describe("Lexer fulltext", () => { | ||||
|     }); | ||||
|  | ||||
|     it("you can use different quotes and other special characters inside quotes", () => { | ||||
|         expect(lexer("'I can use \" or ` or #@=*' without problem").fulltextTokens) | ||||
|             .toEqual(["I can use \" or ` or #@=*", "without", "problem"]); | ||||
|         expect(lexer("'i can use \" or ` or #@=*' without problem").fulltextTokens) | ||||
|             .toEqual(["i can use \" or ` or #@=*", "without", "problem"]); | ||||
|     }); | ||||
|  | ||||
|     it("if quote is not ended then it's just one long token", () => { | ||||
| @@ -56,6 +56,6 @@ describe("Lexer expression", () => { | ||||
|  | ||||
|     it("complex expressions with and, or and parenthesis", () => { | ||||
|         expect(lexer(`# (#label=text OR #second=text) AND @relation`).expressionTokens) | ||||
|             .toEqual(["#", "(", "#label", "=", "text", "OR", "#second", "=", "text", ")", "AND", "@relation"]); | ||||
|             .toEqual(["#", "(", "#label", "=", "text", "or", "#second", "=", "text", ")", "and", "@relation"]); | ||||
|     }); | ||||
| }); | ||||
|   | ||||
| @@ -1,7 +1,122 @@ | ||||
| const searchService = require('../src/services/search/search'); | ||||
| const Note = require('../src/services/note_cache/entities/note'); | ||||
| const Branch = require('../src/services/note_cache/entities/branch'); | ||||
| const Attribute = require('../src/services/note_cache/entities/attribute'); | ||||
| const ParsingContext = require('../src/services/search/parsing_context'); | ||||
| const noteCache = require('../src/services/note_cache/note_cache'); | ||||
| const randtoken = require('rand-token').generator({source: 'crypto'}); | ||||
|  | ||||
| describe("Search", () => { | ||||
|     it("fulltext parser without content", () => { | ||||
| //        searchService. | ||||
|     let rootNote; | ||||
|  | ||||
|     beforeEach(() => { | ||||
|         noteCache.reset(); | ||||
|  | ||||
|         rootNote = new NoteBuilder(new Note(noteCache, {noteId: 'root', title: 'root'})); | ||||
|         new Branch(noteCache, {branchId: 'root', noteId: 'root', parentNoteId: 'none'}); | ||||
|     }); | ||||
|  | ||||
|     it("simple path match", async () => { | ||||
|         rootNote.child( | ||||
|             note("Europe") | ||||
|                 .child( | ||||
|                     note("Austria") | ||||
|                 ) | ||||
|         ); | ||||
|  | ||||
|         const parsingContext = new ParsingContext(); | ||||
|         const searchResults = await searchService.findNotesWithQuery('europe austria', parsingContext); | ||||
|  | ||||
|         expect(searchResults.length).toEqual(1); | ||||
|         expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy(); | ||||
|     }); | ||||
|  | ||||
|     it("only end leafs are results", async () => { | ||||
|         rootNote.child( | ||||
|             note("Europe") | ||||
|                 .child( | ||||
|                     note("Austria") | ||||
|                 ) | ||||
|         ); | ||||
|  | ||||
|         const parsingContext = new ParsingContext(); | ||||
|         const searchResults = await searchService.findNotesWithQuery('europe', parsingContext); | ||||
|  | ||||
|         expect(searchResults.length).toEqual(1); | ||||
|         expect(findNoteByTitle(searchResults, "Europe")).toBeTruthy(); | ||||
|     }); | ||||
|  | ||||
|     it("only end leafs are results", async () => { | ||||
|         rootNote.child( | ||||
|             note("Europe") | ||||
|                 .child( | ||||
|                     note("Austria") | ||||
|                         .label('capital', 'Vienna') | ||||
|                 ) | ||||
|         ); | ||||
|  | ||||
|         const parsingContext = new ParsingContext(); | ||||
|  | ||||
|         const searchResults = await searchService.findNotesWithQuery('Vienna', parsingContext); | ||||
|         expect(searchResults.length).toEqual(1); | ||||
|         expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy(); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| /** @return {Note} */ | ||||
| function findNoteByTitle(searchResults, title) { | ||||
|     return searchResults | ||||
|         .map(sr => noteCache.notes[sr.noteId]) | ||||
|         .find(note => note.title === title); | ||||
| } | ||||
|  | ||||
| class NoteBuilder { | ||||
|     constructor(note) { | ||||
|         this.note = note; | ||||
|     } | ||||
|  | ||||
|     label(name, value) { | ||||
|         new Attribute(noteCache, { | ||||
|             attributeId: id(), | ||||
|             noteId: this.note.noteId, | ||||
|             type: 'label', | ||||
|             name, | ||||
|             value | ||||
|         }); | ||||
|  | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
|     relation(name, note) { | ||||
|         new Attribute(noteCache, { | ||||
|             attributeId: id(), | ||||
|             noteId: this.note.noteId, | ||||
|             type: 'relation', | ||||
|             name, | ||||
|             value: note.noteId | ||||
|         }); | ||||
|  | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
|     child(childNoteBuilder, prefix = "") { | ||||
|         new Branch(noteCache, { | ||||
|             branchId: id(), | ||||
|             noteId: childNoteBuilder.note.noteId, | ||||
|             parentNoteId: this.note.noteId, | ||||
|             prefix | ||||
|         }); | ||||
|  | ||||
|         return this; | ||||
|     } | ||||
| } | ||||
|  | ||||
| function id() { | ||||
|     return randtoken.generate(10); | ||||
| } | ||||
|  | ||||
| function note(title) { | ||||
|     const note = new Note(noteCache, {noteId: id(), title}); | ||||
|  | ||||
|     return new NoteBuilder(note); | ||||
| } | ||||
|   | ||||
| @@ -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