Merge pull request #6455 from Budibase/feature/in-not-in-filter
Add "IN" conditions in data filter
This commit is contained in:
commit
98129809a8
|
@ -42,6 +42,10 @@ export const OperatorOptions = {
|
||||||
value: "notEqual",
|
value: "notEqual",
|
||||||
label: "Does Not Contain",
|
label: "Does Not Contain",
|
||||||
},
|
},
|
||||||
|
In: {
|
||||||
|
value: "oneOf",
|
||||||
|
label: "Is in",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cookie names
|
// Cookie names
|
||||||
|
|
|
@ -14,6 +14,7 @@ export const getValidOperatorsForType = type => {
|
||||||
Op.Like,
|
Op.Like,
|
||||||
Op.Empty,
|
Op.Empty,
|
||||||
Op.NotEmpty,
|
Op.NotEmpty,
|
||||||
|
Op.In,
|
||||||
]
|
]
|
||||||
const numOps = [
|
const numOps = [
|
||||||
Op.Equals,
|
Op.Equals,
|
||||||
|
@ -22,6 +23,7 @@ export const getValidOperatorsForType = type => {
|
||||||
Op.LessThan,
|
Op.LessThan,
|
||||||
Op.Empty,
|
Op.Empty,
|
||||||
Op.NotEmpty,
|
Op.NotEmpty,
|
||||||
|
Op.In,
|
||||||
]
|
]
|
||||||
if (type === "string") {
|
if (type === "string") {
|
||||||
return stringOps
|
return stringOps
|
||||||
|
@ -91,6 +93,7 @@ export const buildLuceneQuery = filter => {
|
||||||
notEmpty: {},
|
notEmpty: {},
|
||||||
contains: {},
|
contains: {},
|
||||||
notContains: {},
|
notContains: {},
|
||||||
|
oneOf: {},
|
||||||
}
|
}
|
||||||
if (Array.isArray(filter)) {
|
if (Array.isArray(filter)) {
|
||||||
filter.forEach(expression => {
|
filter.forEach(expression => {
|
||||||
|
@ -99,9 +102,13 @@ export const buildLuceneQuery = filter => {
|
||||||
if (type === "datetime" && value) {
|
if (type === "datetime" && value) {
|
||||||
value = new Date(value).toISOString()
|
value = new Date(value).toISOString()
|
||||||
}
|
}
|
||||||
if (type === "number") {
|
if (type === "number" && !Array.isArray(value)) {
|
||||||
|
if (operator === "oneOf") {
|
||||||
|
value = value.split(",").map(item => parseFloat(item))
|
||||||
|
} else {
|
||||||
value = parseFloat(value)
|
value = parseFloat(value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (type === "boolean") {
|
if (type === "boolean") {
|
||||||
value = `${value}`?.toLowerCase() === "true"
|
value = `${value}`?.toLowerCase() === "true"
|
||||||
}
|
}
|
||||||
|
@ -139,7 +146,6 @@ export const buildLuceneQuery = filter => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,6 +217,17 @@ export const runLuceneQuery = (docs, query) => {
|
||||||
return docValue == null || docValue === ""
|
return docValue == null || docValue === ""
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Process an includes match (fails if the value is not included)
|
||||||
|
const oneOf = match("oneOf", (docValue, testValue) => {
|
||||||
|
if (typeof testValue === "string") {
|
||||||
|
testValue = testValue.split(",")
|
||||||
|
if (typeof docValue === "number") {
|
||||||
|
testValue = testValue.map(item => parseFloat(item))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !testValue?.includes(docValue)
|
||||||
|
})
|
||||||
|
|
||||||
// Match a document against all criteria
|
// Match a document against all criteria
|
||||||
const docMatch = doc => {
|
const docMatch = doc => {
|
||||||
return (
|
return (
|
||||||
|
@ -220,7 +237,8 @@ export const runLuceneQuery = (docs, query) => {
|
||||||
equalMatch(doc) &&
|
equalMatch(doc) &&
|
||||||
notEqualMatch(doc) &&
|
notEqualMatch(doc) &&
|
||||||
emptyMatch(doc) &&
|
emptyMatch(doc) &&
|
||||||
notEmptyMatch(doc)
|
notEmptyMatch(doc) &&
|
||||||
|
oneOf(doc)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ class QueryBuilder {
|
||||||
notEqual: {},
|
notEqual: {},
|
||||||
empty: {},
|
empty: {},
|
||||||
notEmpty: {},
|
notEmpty: {},
|
||||||
|
oneOf: {},
|
||||||
...base,
|
...base,
|
||||||
}
|
}
|
||||||
this.limit = 50
|
this.limit = 50
|
||||||
|
@ -112,6 +113,11 @@ class QueryBuilder {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addOneOf(key, value) {
|
||||||
|
this.query.oneOf[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.
|
||||||
|
@ -220,6 +226,28 @@ class QueryBuilder {
|
||||||
if (this.query.notEmpty) {
|
if (this.query.notEmpty) {
|
||||||
build(this.query.notEmpty, key => `${key}:["" TO *]`)
|
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.split(",")
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let orStatement = `${builder.preprocess(
|
||||||
|
value[0],
|
||||||
|
allPreProcessingOpts
|
||||||
|
)}`
|
||||||
|
for (let i = 1; i < value.length; i++) {
|
||||||
|
orStatement += ` OR ${builder.preprocess(
|
||||||
|
value[i],
|
||||||
|
allPreProcessingOpts
|
||||||
|
)}`
|
||||||
|
}
|
||||||
|
return `${key}:(${orStatement})`
|
||||||
|
})
|
||||||
|
}
|
||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue