fix(search): Improve search query parsing for implicit AND queries by preserving quoted substrings and better handling free terms, quoted phrases, and field:value tokens.
This commit is contained in:
parent
39d2957b7d
commit
e2dc094afd
@ -1,5 +1,12 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2025-04-22 - 5.13.1 - fix(search)
|
||||||
|
Improve search query parsing for implicit AND queries by preserving quoted substrings and better handling free terms, quoted phrases, and field:value tokens.
|
||||||
|
|
||||||
|
- Replace previous implicit AND logic with tokenization that preserves quoted substrings
|
||||||
|
- Support both free term and field:value tokens with wildcards inside quotes
|
||||||
|
- Ensure errors are thrown for non-searchable fields in field-specific queries
|
||||||
|
|
||||||
## 2025-04-22 - 5.13.0 - feat(search)
|
## 2025-04-22 - 5.13.0 - feat(search)
|
||||||
Improve search query handling and update documentation
|
Improve search query handling and update documentation
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartdata',
|
name: '@push.rocks/smartdata',
|
||||||
version: '5.13.0',
|
version: '5.13.1',
|
||||||
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
||||||
}
|
}
|
||||||
|
@ -419,62 +419,52 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
|||||||
const orConds = searchableFields.map((f) => ({ [f]: { $regex: pattern, $options: 'i' } }));
|
const orConds = searchableFields.map((f) => ({ [f]: { $regex: pattern, $options: 'i' } }));
|
||||||
return await (this as any).execQuery({ $or: orConds }, opts);
|
return await (this as any).execQuery({ $or: orConds }, opts);
|
||||||
}
|
}
|
||||||
// implicit AND: combine free terms and field:value terms (with or without wildcards)
|
// implicit AND for multiple tokens: free terms, quoted phrases, and field:values
|
||||||
const parts = q.split(/\s+/);
|
{
|
||||||
const hasColon = parts.some((t) => t.includes(':'));
|
// Split query into tokens, preserving quoted substrings
|
||||||
|
const rawTokens = q.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || [];
|
||||||
|
// Only apply when more than one token and no boolean operators or grouping
|
||||||
if (
|
if (
|
||||||
parts.length > 1 && hasColon &&
|
rawTokens.length > 1 &&
|
||||||
!q.includes(' AND ') && !q.includes(' OR ') && !q.includes(' NOT ') &&
|
!/(\bAND\b|\bOR\b|\bNOT\b|\(|\))/i.test(q) &&
|
||||||
!q.includes('(') && !q.includes(')') &&
|
!/\[|\]/.test(q)
|
||||||
!q.includes('[') && !q.includes(']') &&
|
|
||||||
!q.includes('"') && !q.includes("'")
|
|
||||||
) {
|
) {
|
||||||
const andConds = parts.map((term) => {
|
const andConds: any[] = [];
|
||||||
const m = term.match(/^(\w+):(.+)$/);
|
for (let token of rawTokens) {
|
||||||
if (m) {
|
// field:value token
|
||||||
const field = m[1];
|
const fv = token.match(/^(\w+):(.+)$/);
|
||||||
const value = m[2];
|
if (fv) {
|
||||||
|
const field = fv[1];
|
||||||
|
let value = fv[2];
|
||||||
if (!searchableFields.includes(field)) {
|
if (!searchableFields.includes(field)) {
|
||||||
throw new Error(`Field '${field}' is not searchable for class ${this.name}`);
|
throw new Error(`Field '${field}' is not searchable for class ${this.name}`);
|
||||||
}
|
}
|
||||||
|
// Strip surrounding quotes if present
|
||||||
|
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||||
|
value = value.slice(1, -1);
|
||||||
|
}
|
||||||
|
// Wildcard search?
|
||||||
if (value.includes('*') || value.includes('?')) {
|
if (value.includes('*') || value.includes('?')) {
|
||||||
// wildcard field search
|
|
||||||
const escaped = value.replace(/([.+^${}()|[\\]\\])/g, '\\$1');
|
const escaped = value.replace(/([.+^${}()|[\\]\\])/g, '\\$1');
|
||||||
const pattern = escaped.replace(/\*/g, '.*').replace(/\?/g, '.');
|
const pattern = escaped.replace(/\*/g, '.*').replace(/\?/g, '.');
|
||||||
return { [field]: { $regex: pattern, $options: 'i' } };
|
andConds.push({ [field]: { $regex: pattern, $options: 'i' } });
|
||||||
|
} else {
|
||||||
|
andConds.push({ [field]: value });
|
||||||
|
}
|
||||||
|
} else if ((token.startsWith('"') && token.endsWith('"')) || (token.startsWith("'") && token.endsWith("'"))) {
|
||||||
|
// Quoted free phrase across all fields
|
||||||
|
const phrase = token.slice(1, -1);
|
||||||
|
const parts = phrase.split(/\s+/).map((t) => escapeForRegex(t));
|
||||||
|
const pattern = parts.join('\\s+');
|
||||||
|
andConds.push({ $or: searchableFields.map((f) => ({ [f]: { $regex: pattern, $options: 'i' } })) });
|
||||||
|
} else {
|
||||||
|
// Free term across all fields
|
||||||
|
const esc = escapeForRegex(token);
|
||||||
|
andConds.push({ $or: searchableFields.map((f) => ({ [f]: { $regex: esc, $options: 'i' } })) });
|
||||||
}
|
}
|
||||||
// exact field:value
|
|
||||||
return { [field]: value };
|
|
||||||
}
|
}
|
||||||
// free term -> regex across all searchable fields
|
|
||||||
const esc = escapeForRegex(term);
|
|
||||||
return { $or: searchableFields.map((f) => ({ [f]: { $regex: esc, $options: 'i' } })) };
|
|
||||||
});
|
|
||||||
return await (this as any).execQuery({ $and: andConds }, opts);
|
return await (this as any).execQuery({ $and: andConds }, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// free term and quoted field phrase (exact or wildcard), e.g. 'term field:"phrase"' or 'term field:"ph*se"'
|
|
||||||
const freeWithQuotedField = q.match(/^(\S+)\s+(\w+):"(.+)"$/);
|
|
||||||
if (freeWithQuotedField) {
|
|
||||||
const freeTerm = freeWithQuotedField[1];
|
|
||||||
const field = freeWithQuotedField[2];
|
|
||||||
let phrase = freeWithQuotedField[3];
|
|
||||||
if (!searchableFields.includes(field)) {
|
|
||||||
throw new Error(`Field '${field}' is not searchable for class ${this.name}`);
|
|
||||||
}
|
|
||||||
// free term condition across all searchable fields
|
|
||||||
const freeEsc = escapeForRegex(freeTerm);
|
|
||||||
const freeCond = { $or: searchableFields.map((f) => ({ [f]: { $regex: freeEsc, $options: 'i' } })) };
|
|
||||||
// field condition: exact match or wildcard pattern
|
|
||||||
let fieldCond;
|
|
||||||
if (phrase.includes('*') || phrase.includes('?')) {
|
|
||||||
const escaped = phrase.replace(/([.+^${}()|[\\]\\])/g, '\\$1');
|
|
||||||
const pattern = escaped.replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
||||||
fieldCond = { [field]: { $regex: pattern, $options: 'i' } };
|
|
||||||
} else {
|
|
||||||
fieldCond = { [field]: phrase };
|
|
||||||
}
|
|
||||||
return await (this as any).execQuery({ $and: [freeCond, fieldCond] }, opts);
|
|
||||||
}
|
}
|
||||||
// detect advanced Lucene syntax: field:value, wildcards, boolean, grouping
|
// detect advanced Lucene syntax: field:value, wildcards, boolean, grouping
|
||||||
const luceneSyntax = /(\w+:[^\s]+)|\*|\?|\bAND\b|\bOR\b|\bNOT\b|\(|\)/;
|
const luceneSyntax = /(\w+:[^\s]+)|\*|\?|\bAND\b|\bOR\b|\bNOT\b|\(|\)/;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user