1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

Fixing search query parser

This commit is contained in:
Patrik J. Braun 2021-03-20 23:31:39 +01:00
parent 2137fc71d2
commit c5b657a96a
3 changed files with 10 additions and 10 deletions

View File

@ -106,10 +106,10 @@ export class SearchQueryParser {
} }
} }
if (str.startsWith(this.keywords.someOf + ':') || if (str.startsWith(this.keywords.someOf + ':') ||
new RegExp(/^\d*-of:/).test(str)) { new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').test(str)) {
const prefix = str.startsWith(this.keywords.someOf + ':') ? const prefix = str.startsWith(this.keywords.someOf + ':') ?
this.keywords.someOf + ':' : this.keywords.someOf + ':' :
new RegExp(/^\d*-of:/).exec(str)[0]; new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').exec(str)[0];
let tmpList: any = this.parse(str.slice(prefix.length + 1, -1), false); // trim brackets let tmpList: any = this.parse(str.slice(prefix.length + 1, -1), false); // trim brackets
// console.log(JSON.stringify(tmpList, null, 4)); // console.log(JSON.stringify(tmpList, null, 4));
const unfoldList = (q: SearchListQuery): SearchQueryDTO[] => { const unfoldList = (q: SearchListQuery): SearchQueryDTO[] => {
@ -127,7 +127,7 @@ export class SearchQueryParser {
type: SearchQueryTypes.SOME_OF, type: SearchQueryTypes.SOME_OF,
list: tmpList list: tmpList
}; };
if (new RegExp('/^\d*-' + this.keywords.NSomeOf + ':/').test(str)) { if (new RegExp('^\\d*-' + this.keywords.NSomeOf + ':').test(str)) {
ret.min = parseInt(new RegExp(/^\d*/).exec(str)[0], 10); ret.min = parseInt(new RegExp(/^\d*/).exec(str)[0], 10);
} }
return ret; return ret;
@ -170,8 +170,8 @@ export class SearchQueryParser {
value: parseInt(str.slice((this.keywords.maxResolution + ':').length), 10) value: parseInt(str.slice((this.keywords.maxResolution + ':').length), 10)
}; };
} }
if (new RegExp('/^\d*-' + this.keywords.kmFrom + ':/').test(str)) { if (new RegExp('^\\d*-' + this.keywords.kmFrom + ':').test(str)) {
let from = str.slice(new RegExp('/^\d*-' + this.keywords.kmFrom + ':/').exec(str)[0].length); let from = str.slice(new RegExp('^\\d*-' + this.keywords.kmFrom + ':').exec(str)[0].length);
if (from.charAt(0) === '(' && from.charAt(from.length - 1) === ')') { if (from.charAt(0) === '(' && from.charAt(from.length - 1) === ')') {
from = from.slice(1, from.length - 1); from = from.slice(1, from.length - 1);
} }
@ -279,12 +279,12 @@ export class SearchQueryParser {
return ''; return '';
} }
if ((<TextSearch>query).matchType === TextSearchQueryMatchTypes.exact_match) { if ((<TextSearch>query).matchType === TextSearchQueryMatchTypes.exact_match) {
return SearchQueryTypes[query.type] + ':"' + (<TextSearch>query).text + '"'; return (<any>this.keywords)[SearchQueryTypes[query.type]] + ':"' + (<TextSearch>query).text + '"';
} else if ((<TextSearch>query).text.indexOf(' ') !== -1) { } else if ((<TextSearch>query).text.indexOf(' ') !== -1) {
return SearchQueryTypes[query.type] + ':(' + (<TextSearch>query).text + ')'; return (<any>this.keywords)[SearchQueryTypes[query.type]] + ':(' + (<TextSearch>query).text + ')';
} }
return SearchQueryTypes[query.type] + ':' + (<TextSearch>query).text; return (<any>this.keywords)[SearchQueryTypes[query.type]] + ':' + (<TextSearch>query).text;
default: default:
throw new Error('Unknown type: ' + query.type); throw new Error('Unknown type: ' + query.type);

View File

@ -6,7 +6,7 @@ import {SearchQueryDTO} from '../../../../../common/entities/SearchQueryDTO';
export class SearchQueryParserService { export class SearchQueryParserService {
public readonly keywords: QueryKeywords = { public readonly keywords: QueryKeywords = {
NSomeOf: '-of', NSomeOf: 'of',
and: 'and', and: 'and',
caption: 'caption', caption: 'caption',
directory: 'directory', directory: 'directory',

View File

@ -18,7 +18,7 @@ import {
} from '../../../src/common/entities/SearchQueryDTO'; } from '../../../src/common/entities/SearchQueryDTO';
import {QueryKeywords, SearchQueryParser} from '../../../src/common/SearchQueryParser'; import {QueryKeywords, SearchQueryParser} from '../../../src/common/SearchQueryParser';
describe('SearchQueryDTO', () => { describe('SearchQueryParser', () => {
const keywords: QueryKeywords = { const keywords: QueryKeywords = {
NSomeOf: '-of', NSomeOf: '-of',