Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
498f586ddb | |||
6c50bd23ec | |||
419eb163f4 | |||
75aeb12e81 |
15
changelog.md
15
changelog.md
@ -1,5 +1,20 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-04-22 - 5.11.4 - fix(search)
|
||||
Implement implicit AND logic for mixed simple term and field:value queries in search
|
||||
|
||||
- Added a new branch to detect and handle search queries that mix field:value pairs with plain terms without explicit operators
|
||||
- Builds an implicit $and filter when query parts contain colon(s) but lack explicit boolean operators or quotes
|
||||
- Ensures proper parsing and improved robustness of search filters
|
||||
|
||||
## 2025-04-22 - 5.11.3 - fix(lucene adapter and search tests)
|
||||
Improve range query parsing in Lucene adapter and expand search test coverage
|
||||
|
||||
- Added a new 'testSearch' script in package.json to run search tests.
|
||||
- Introduced advanced search tests for range queries and combined field filters in test/search.advanced.ts.
|
||||
- Enhanced robustness tests in test/search.ts for wildcard and empty query scenarios.
|
||||
- Fixed token validation in the parseRange method of the Lucene adapter to ensure proper error handling.
|
||||
|
||||
## 2025-04-21 - 5.11.2 - fix(readme)
|
||||
Update readme to clarify usage of searchable fields retrieval
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@push.rocks/smartdata",
|
||||
"version": "5.11.2",
|
||||
"version": "5.11.4",
|
||||
"private": false,
|
||||
"description": "An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -8,6 +8,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "tstest test/",
|
||||
"testSearch": "tsx test/test.search.ts",
|
||||
"build": "tsbuild --web --allowimplicitany",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
|
@ -172,6 +172,21 @@ tap.test('grouping: (Furniture OR Electronics) AND Chair', async () => {
|
||||
expect(names).toEqual(['Gaming Chair', 'Office Chair']);
|
||||
});
|
||||
|
||||
// Additional range and combined query tests
|
||||
tap.test('range query price:[30 TO 300] returns expected products', async () => {
|
||||
const res = await Product.search('price:[30 TO 300]');
|
||||
// Expect products with price between 30 and 300 inclusive: Day Light Lamp, Gaming Chair, Office Chair, AirPods
|
||||
expect(res.length).toEqual(4);
|
||||
const names = res.map((r) => r.name).sort();
|
||||
expect(names).toEqual(['AirPods', 'Day Light Lamp', 'Gaming Chair', 'Office Chair']);
|
||||
});
|
||||
|
||||
tap.test('should filter category and price range', async () => {
|
||||
const res = await Product.search('category:Lighting AND price:[30 TO 40]');
|
||||
expect(res.length).toEqual(1);
|
||||
expect(res[0].name).toEqual('Day Light Lamp');
|
||||
});
|
||||
|
||||
// Teardown
|
||||
tap.test('cleanup advanced search database', async () => {
|
||||
await testDb.mongoDb.dropDatabase();
|
||||
|
@ -257,6 +257,26 @@ tap.test('should search hyphenated terms "E-reader"', async () => {
|
||||
expect(ereaderResults[0].name).toEqual('Kindle Paperwhite');
|
||||
});
|
||||
|
||||
// Additional robustness tests
|
||||
tap.test('should return all products for empty search', async () => {
|
||||
const searchResults = await Product.search('');
|
||||
const allProducts = await Product.getInstances({});
|
||||
expect(searchResults.length).toEqual(allProducts.length);
|
||||
});
|
||||
|
||||
tap.test('should support wildcard plain term across all fields', async () => {
|
||||
const results = await Product.search('*book*');
|
||||
const names = results.map((r) => r.name).sort();
|
||||
expect(names).toEqual(['Harry Potter', 'Kindle Paperwhite', 'MacBook Pro']);
|
||||
});
|
||||
|
||||
tap.test('should support wildcard plain term with question mark pattern', async () => {
|
||||
const results = await Product.search('?one?');
|
||||
const names = results.map((r) => r.name).sort();
|
||||
expect(names).toEqual(['Galaxy S21', 'iPhone 12']);
|
||||
});
|
||||
|
||||
// Close database connection
|
||||
tap.test('close database connection', async () => {
|
||||
await testDb.mongoDb.dropDatabase();
|
||||
await testDb.close();
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartdata',
|
||||
version: '5.11.2',
|
||||
version: '5.11.4',
|
||||
description: 'An advanced library for NoSQL data organization and manipulation using TypeScript with support for MongoDB, data validation, collections, and custom data types.'
|
||||
}
|
||||
|
@ -379,6 +379,34 @@ export class SmartDataDbDoc<T extends TImplements, TImplements, TManager extends
|
||||
const orConds = searchableFields.map((f) => ({ [f]: { $regex: pattern, $options: 'i' } }));
|
||||
return await (this as any).getInstances({ $or: orConds });
|
||||
}
|
||||
// implicit AND for mixed simple term + field:value queries (no explicit operators)
|
||||
const parts = q.split(/\s+/);
|
||||
const hasColon = parts.some((t) => t.includes(':'));
|
||||
// implicit AND for mixed simple term + field:value queries (no explicit operators or range syntax)
|
||||
if (
|
||||
parts.length > 1 && hasColon &&
|
||||
!q.includes(' AND ') && !q.includes(' OR ') && !q.includes(' NOT ') &&
|
||||
!q.includes('(') && !q.includes(')') && !q.includes('[') && !q.includes(']') &&
|
||||
!q.includes('"') && !q.includes("'") &&
|
||||
!q.includes('*') && !q.includes('?')
|
||||
) {
|
||||
const andConds = parts.map((term) => {
|
||||
const m = term.match(/^(\\w+):([^"'\\*\\?\\s]+)$/);
|
||||
if (m) {
|
||||
const field = m[1];
|
||||
const value = m[2];
|
||||
if (!searchableFields.includes(field)) {
|
||||
throw new Error(`Field '${field}' is not searchable for class ${this.name}`);
|
||||
}
|
||||
return { [field]: value };
|
||||
} else {
|
||||
const esc = escapeForRegex(term);
|
||||
const ors = searchableFields.map((f) => ({ [f]: { $regex: esc, $options: 'i' } }));
|
||||
return { $or: ors };
|
||||
}
|
||||
});
|
||||
return await (this as any).getInstances({ $and: andConds });
|
||||
}
|
||||
// detect advanced Lucene syntax: field:value, wildcards, boolean, grouping
|
||||
const luceneSyntax = /(\w+:[^\s]+)|\*|\?|\bAND\b|\bOR\b|\bNOT\b|\(|\)/;
|
||||
if (luceneSyntax.test(q)) {
|
||||
|
@ -290,11 +290,11 @@ export class LuceneParser {
|
||||
const includeLower = this.tokens[this.pos] === '[';
|
||||
const includeUpper = this.tokens[this.pos + 4] === ']';
|
||||
|
||||
this.pos++; // Skip open bracket
|
||||
|
||||
// Ensure tokens for lower, TO, upper, and closing bracket exist
|
||||
if (this.pos + 4 >= this.tokens.length) {
|
||||
throw new Error('Invalid range query syntax');
|
||||
}
|
||||
this.pos++; // Skip open bracket
|
||||
|
||||
const lower = this.tokens[this.pos];
|
||||
this.pos++;
|
||||
|
Reference in New Issue
Block a user