mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	allow searching with noteId & fix combining fulltext and other conditions with OR
This commit is contained in:
		
							parent
							
								
									26621c0318
								
							
						
					
					
						commit
						5bda254184
					
				@ -5,6 +5,7 @@ const VIRTUAL_ATTRIBUTES = [
 | 
				
			|||||||
    "dateModified",
 | 
					    "dateModified",
 | 
				
			||||||
    "utcDateCreated",
 | 
					    "utcDateCreated",
 | 
				
			||||||
    "utcDateModified",
 | 
					    "utcDateModified",
 | 
				
			||||||
 | 
					    "noteId",
 | 
				
			||||||
    "isProtected",
 | 
					    "isProtected",
 | 
				
			||||||
    "title",
 | 
					    "title",
 | 
				
			||||||
    "content",
 | 
					    "content",
 | 
				
			||||||
@ -37,20 +38,11 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
 | 
				
			|||||||
            const alias = "note_contents";
 | 
					            const alias = "note_contents";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (!(alias in joins)) {
 | 
					            if (!(alias in joins)) {
 | 
				
			||||||
                joins[alias] = `JOIN note_contents ON note_contents.noteId = notes.noteId`;
 | 
					                joins[alias] = `LEFT JOIN note_contents ON note_contents.noteId = notes.noteId`;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            accessor = `${alias}.${property}`;
 | 
					            accessor = `${alias}.${property}`;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else if (property === 'text') {
 | 
					 | 
				
			||||||
            const alias = "note_fulltext";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (!(alias in joins)) {
 | 
					 | 
				
			||||||
                joins[alias] = `JOIN note_fulltext ON note_fulltext.noteId = notes.noteId`;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            accessor = alias;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            accessor = "notes." + property;
 | 
					            accessor = "notes." + property;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -92,17 +84,11 @@ module.exports = function(filters, selectedColumns = 'notes.*') {
 | 
				
			|||||||
        else if (filter.operator === '=' || filter.operator === '!=') {
 | 
					        else if (filter.operator === '=' || filter.operator === '!=') {
 | 
				
			||||||
            if (filter.name === 'text') {
 | 
					            if (filter.name === 'text') {
 | 
				
			||||||
                const safeSearchText = utils.sanitizeSql(filter.value);
 | 
					                const safeSearchText = utils.sanitizeSql(filter.value);
 | 
				
			||||||
                let condition = accessor + ' ' + `MATCH '${safeSearchText}'`;
 | 
					                const not = filter.operator.includes("!") ? "NOT" : "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (filter.operator.includes("!")) {
 | 
					                // fulltext needs to use subselect because fulltext doesn't support OR operations at all
 | 
				
			||||||
                    // not supported!
 | 
					                // which makes it impossible to combine more operations together
 | 
				
			||||||
                }
 | 
					                where += `notes.noteId ${not} IN (SELECT noteId FROM note_fulltext WHERE note_fulltext MATCH '${safeSearchText}')`;
 | 
				
			||||||
                else if (orderBy.length === 0) {
 | 
					 | 
				
			||||||
                    // if there's a positive full text search and there's no defined order then order by rank
 | 
					 | 
				
			||||||
                    orderBy.push("rank");
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                where += condition;
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
                where += `${accessor} ${filter.operator} ?`;
 | 
					                where += `${accessor} ${filter.operator} ?`;
 | 
				
			||||||
 | 
				
			|||||||
@ -51,6 +51,12 @@ module.exports = function (searchText) {
 | 
				
			|||||||
                name: 'text',
 | 
					                name: 'text',
 | 
				
			||||||
                operator: '=',
 | 
					                operator: '=',
 | 
				
			||||||
                value: searchText
 | 
					                value: searchText
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                relation: 'or',
 | 
				
			||||||
 | 
					                name: 'noteId',
 | 
				
			||||||
 | 
					                operator: '=',
 | 
				
			||||||
 | 
					                value: searchText
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -91,7 +91,7 @@ function decryptNoteRevision(hist) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (e) {
 | 
					    catch (e) {
 | 
				
			||||||
        throw new Error(`Decryption failed for note ${hist.noteId}: ` + e.message + " " + e.stack);
 | 
					        throw new Error(`Decryption failed for note ${hist.noteId}, revision ${hist.noteRevisionId}: ` + e.message + " " + e.stack);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user