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",
label: "Is in",
},
ContainsAny: {
value: "containsAny",
label: "Has any"
},
}
// Cookie names

View File

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

View File

@ -21,6 +21,7 @@ class QueryBuilder {
oneOf: {},
contains: {},
notContains: {},
containsAny: {},
...base,
}
this.limit = 50
@ -131,6 +132,11 @@ class QueryBuilder {
return this
}
addContainsAny(key, value) {
this.query.containsAny[key] = value
return this
}
/**
* Preprocesses a value before going into a lucene search.
* Transforms strings to lowercase and wraps strings and bools in quotes.
@ -176,21 +182,25 @@ class QueryBuilder {
return `${key}:${builder.preprocess(value, allPreProcessingOpts)}`
}
const contains = (key, value) => {
const contains = (key, value, mode = "AND") => {
if (!Array.isArray(value) || value.length === 0) {
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++) {
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) => {
return "*:* AND NOT " + contains(key, value)
}
const containsAny = (key, value) => {
return contains(key, value, "OR")
}
const oneOf = (key, value) => {
if (!Array.isArray(value)) {
if (typeof value === "string") {
@ -292,6 +302,9 @@ class QueryBuilder {
if (this.query.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
if (tableId) {
query = `(${query})`