update runLuceneQuery in client to allow for all filter matching

This commit is contained in:
Peter Clement 2024-02-28 08:34:41 +00:00
parent 2d1efe57d7
commit 2933571c62
1 changed files with 44 additions and 15 deletions

View File

@ -390,23 +390,52 @@ export const runLuceneQuery = (docs: any[], query?: SearchQuery) => {
} }
) )
// Match a document against all criteria
const docMatch = (doc: any) => { const docMatch = (doc: any) => {
return ( // Determine active filters based on query object
stringMatch(doc) && const activeFilterKeys = Object.entries(query || {})
fuzzyMatch(doc) && .filter(
rangeMatch(doc) && ([key, value]) =>
equalMatch(doc) && !["allOr", "onEmptyFilter"].includes(key) &&
notEqualMatch(doc) && Object.keys(value).length > 0
emptyMatch(doc) && )
notEmptyMatch(doc) && .map(([key]) => key)
oneOf(doc) &&
contains(doc) &&
containsAny(doc) &&
notContains(doc)
)
}
// Apply filters dynamically based on activeFilterKeys
const results = activeFilterKeys.map(filterKey => {
switch (filterKey) {
case "string":
return stringMatch(doc)
case "fuzzy":
return fuzzyMatch(doc)
case "range":
return rangeMatch(doc)
case "equal":
return equalMatch(doc)
case "notEqual":
return notEqualMatch(doc)
case "empty":
return emptyMatch(doc)
case "notEmpty":
return notEmptyMatch(doc)
case "oneOf":
return oneOf(doc)
case "contains":
return contains(doc)
case "containsAny":
return containsAny(doc)
case "notContains":
return notContains(doc)
default:
return true // If the filter type is not recognized, default to true (assuming pass)
}
})
if (query!.allOr) {
return results.some(result => result === true)
} else {
return results.every(result => result === true)
}
}
// Process all docs // Process all docs
return docs.filter(docMatch) return docs.filter(docMatch)
} }