refactor: replace basic search with SearchOrchestratorService for improved tag filtering and search index
This commit is contained in:
@@ -37,6 +37,12 @@ describe('Search Parser', () => {
|
||||
expect(parsed.isEmpty).toBe(false);
|
||||
});
|
||||
|
||||
it('queryToPredicate matches tag operator', () => {
|
||||
const predicate = queryToPredicate(parseSearchQuery('tag:test'));
|
||||
expect(predicate(createContext({ tags: ['#test', '#other'] }))).toBe(true);
|
||||
expect(predicate(createContext({ tags: ['#other'] }))).toBe(false);
|
||||
});
|
||||
|
||||
it('queryToPredicate matches simple text', () => {
|
||||
const predicate = queryToPredicate(parseSearchQuery('hello'));
|
||||
expect(predicate(createContext())).toBe(true);
|
||||
|
||||
@@ -68,101 +68,20 @@ export function queryToPredicate(parsed: ParsedQuery, options?: SearchOptions):
|
||||
*/
|
||||
function tokenize(query: string): string[] {
|
||||
const tokens: string[] = [];
|
||||
let current = '';
|
||||
let i = 0;
|
||||
|
||||
while (i < query.length) {
|
||||
const char = query[i];
|
||||
|
||||
// Handle quoted strings
|
||||
if (char === '"') {
|
||||
// If we are inside a prefix token like file: or path:, attach the quoted part to current
|
||||
if (current.includes(':') && !current.includes(' ')) {
|
||||
let quoted = '"';
|
||||
i++;
|
||||
while (i < query.length && query[i] !== '"') {
|
||||
quoted += query[i];
|
||||
i++;
|
||||
}
|
||||
if (i < query.length && query[i] === '"') {
|
||||
quoted += '"';
|
||||
i++;
|
||||
}
|
||||
current += quoted;
|
||||
continue;
|
||||
} else {
|
||||
if (current) {
|
||||
tokens.push(current);
|
||||
current = '';
|
||||
}
|
||||
let quoted = '';
|
||||
i++;
|
||||
while (i < query.length && query[i] !== '"') {
|
||||
quoted += query[i];
|
||||
i++;
|
||||
}
|
||||
if (i < query.length && query[i] === '"') {
|
||||
i++;
|
||||
}
|
||||
if (quoted) {
|
||||
tokens.push(`"${quoted}"`);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// This regex handles:
|
||||
// - quoted strings (double and single)
|
||||
// - regex patterns /.../
|
||||
// - parentheses
|
||||
// - property searches like [prop]:"value"
|
||||
// - operators and words
|
||||
const regex = /\s*("([^"]*)"|'([^']*)'|\/([^\/]*)\/|\(|\)|-?\[[^\]]*\]:?"[^"]*"|-?\[[^\]]*\]|-?[^\s\(\)]+)/g;
|
||||
let match;
|
||||
while ((match = regex.exec(query)) !== null) {
|
||||
if (match[1]) {
|
||||
tokens.push(match[1]);
|
||||
}
|
||||
|
||||
// Handle regex patterns /.../
|
||||
if (char === '/' && (current === '' || current.endsWith(' '))) {
|
||||
if (current) {
|
||||
tokens.push(current);
|
||||
current = '';
|
||||
}
|
||||
let regex = '/';
|
||||
i++;
|
||||
while (i < query.length && query[i] !== '/') {
|
||||
regex += query[i];
|
||||
i++;
|
||||
}
|
||||
if (i < query.length) {
|
||||
regex += '/';
|
||||
i++;
|
||||
}
|
||||
if (regex.length > 2) {
|
||||
tokens.push(regex);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle parentheses
|
||||
if (char === '(' || char === ')') {
|
||||
if (current) {
|
||||
tokens.push(current);
|
||||
current = '';
|
||||
}
|
||||
tokens.push(char);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle spaces
|
||||
if (char === ' ') {
|
||||
if (current) {
|
||||
tokens.push(current);
|
||||
current = '';
|
||||
}
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
current += char;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (current) {
|
||||
tokens.push(current);
|
||||
}
|
||||
|
||||
return tokens.filter(t => t.length > 0);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user