Invert boolean filter expressions to consider nulls as false

This commit is contained in:
Andrew Kingston 2021-06-09 16:41:15 +01:00
parent aed973681e
commit 6e2823a4c0
1 changed files with 20 additions and 2 deletions

View File

@ -13,7 +13,9 @@ export const buildLuceneQuery = filter => {
notEmpty: {},
}
if (Array.isArray(filter)) {
filter.forEach(({ operator, field, type, value }) => {
// Build up proper range filters
filter.forEach(expression => {
const { operator, field, type, value } = expression
if (operator.startsWith("range")) {
if (!query.range[field]) {
query.range[field] = {
@ -33,10 +35,26 @@ export const buildLuceneQuery = filter => {
query.range[field].high = value
}
} else if (query[operator]) {
if (type === "boolean") {
// Transform boolean filters to cope with null.
// "equals false" needs to be "not equals true"
// "not equals false" needs to be "equals true"
if (operator === "equal" && value === "false") {
query.notEqual[field] = "true"
} else if (operator === "notEqual" && value === "false") {
query.equal[field] = "true"
} else {
query[operator][field] = value
}
} else {
query[operator][field] = value
}
}
})
}
console.log(query)
return query
}