Has any filter InternalDB

This commit is contained in:
Mel O'Hagan 2022-07-27 13:02:46 +01:00
parent 413bd55b94
commit b5dbeb49e4
3 changed files with 23 additions and 5 deletions

View File

@ -46,6 +46,10 @@ export const OperatorOptions = {
value: "oneOf", value: "oneOf",
label: "Is in", label: "Is in",
}, },
ContainsAny: {
value: "containsAny",
label: "Has any"
},
} }
// Cookie names // Cookie names

View File

@ -32,7 +32,7 @@ export const getValidOperatorsForType = type => {
} else if (type === "options") { } else if (type === "options") {
return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty] return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty]
} else if (type === "array") { } else if (type === "array") {
return [Op.Contains, Op.NotContains, Op.Empty, Op.NotEmpty] return [Op.Contains, Op.NotContains, Op.Empty, Op.NotEmpty, Op.ContainsAny]
} else if (type === "boolean") { } else if (type === "boolean") {
return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty] return [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty]
} else if (type === "longform") { } else if (type === "longform") {
@ -94,6 +94,7 @@ export const buildLuceneQuery = filter => {
contains: {}, contains: {},
notContains: {}, notContains: {},
oneOf: {}, oneOf: {},
containsAny: {},
} }
if (Array.isArray(filter)) { if (Array.isArray(filter)) {
filter.forEach(expression => { filter.forEach(expression => {

View File

@ -21,6 +21,7 @@ class QueryBuilder {
oneOf: {}, oneOf: {},
contains: {}, contains: {},
notContains: {}, notContains: {},
containsAny: {},
...base, ...base,
} }
this.limit = 50 this.limit = 50
@ -131,6 +132,11 @@ class QueryBuilder {
return this return this
} }
addContainsAny(key, value) {
this.query.containsAny[key] = value
return this
}
/** /**
* Preprocesses a value before going into a lucene search. * Preprocesses a value before going into a lucene search.
* Transforms strings to lowercase and wraps strings and bools in quotes. * Transforms strings to lowercase and wraps strings and bools in quotes.
@ -176,21 +182,25 @@ class QueryBuilder {
return `${key}:${builder.preprocess(value, allPreProcessingOpts)}` return `${key}:${builder.preprocess(value, allPreProcessingOpts)}`
} }
const contains = (key, value) => { const contains = (key, value, mode = "AND") => {
if (!Array.isArray(value) || value.length === 0) { if (!Array.isArray(value) || value.length === 0) {
return null return null
} }
let andStatement = `${builder.preprocess(value[0], { escape: true })}` let statement = `${builder.preprocess(value[0], { escape: true })}`
for (let i = 1; i < value.length; i++) { for (let i = 1; i < value.length; i++) {
andStatement += ` AND ${builder.preprocess(value[i], { escape: true })}` statement += ` ${mode} ${builder.preprocess(value[i], { escape: true })}`
} }
return `${key}:(${andStatement})` return `${key}:(${statement})`
} }
const notContains = (key, value) => { const notContains = (key, value) => {
return "*:* AND NOT " + contains(key, value) return "*:* AND NOT " + contains(key, value)
} }
const containsAny = (key, value) => {
return contains(key, value, "OR")
}
const oneOf = (key, value) => { const oneOf = (key, value) => {
if (!Array.isArray(value)) { if (!Array.isArray(value)) {
if (typeof value === "string") { if (typeof value === "string") {
@ -292,6 +302,9 @@ class QueryBuilder {
if (this.query.notContains) { if (this.query.notContains) {
build(this.query.notContains, notContains) build(this.query.notContains, notContains)
} }
if (this.query.containsAny) {
build(this.query.containsAny, containsAny)
}
// make sure table ID is always added as an AND // make sure table ID is always added as an AND
if (tableId) { if (tableId) {
query = `(${query})` query = `(${query})`