diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.js index 5f1dc25faa..23cbdb4bec 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.js @@ -17,6 +17,7 @@ class QueryBuilder { notEqual: {}, empty: {}, notEmpty: {}, + oneOf: {}, ...base, } this.limit = 50 @@ -112,6 +113,11 @@ class QueryBuilder { return this } + addOneOf(key, value) { + this.query.oneOf[key] = value + return this + } + /** * Preprocesses a value before going into a lucene search. * Transforms strings to lowercase and wraps strings and bools in quotes. @@ -220,6 +226,28 @@ class QueryBuilder { if (this.query.notEmpty) { build(this.query.notEmpty, key => `${key}:["" TO *]`) } + if (this.query.oneOf) { + build(this.query.oneOf, (key, value) => { + if (!Array.isArray(value)) { + if (typeof value === "string") { + value = value.replace(/(\s)*,(\s)*/g, ",").split(",") + } else { + return "" + } + } + const preprocess = item => { + return builder.preprocess(item, { + escape: true, + lowercase: true, + }) + } + let orStatement = `"${preprocess(value[0])}"` + for (let i = 1; i < value.length; i++) { + orStatement += ` OR "${preprocess(value[i])}"` + } + return `${key}:(${orStatement})` + }) + } return query }