Typing improvements and fixing issue detected by test cases (old lucene syntax).

This commit is contained in:
mike12345567 2024-09-11 12:21:19 +01:00
parent 9ea74dcb45
commit 9ea3aca5e4
2 changed files with 16 additions and 11 deletions

View File

@ -39,9 +39,9 @@ export async function searchView(
// Enrich saved query with ephemeral query params. // Enrich saved query with ephemeral query params.
// We prevent searching on any fields that are saved as part of the query, as // We prevent searching on any fields that are saved as part of the query, as
// that could let users find rows they should not be allowed to access. // that could let users find rows they should not be allowed to access.
let query: any = supportsLogicalOperators let query = supportsLogicalOperators
? dataFilters.buildQuery(view.query) ? dataFilters.buildQuery(view.query)
: dataFilters.buildQueryLegacy(view.query as SearchFilter[]) : dataFilters.buildQueryLegacy(view.query)
delete query?.onEmptyFilter delete query?.onEmptyFilter
@ -51,8 +51,10 @@ export async function searchView(
if (!supportsLogicalOperators) { if (!supportsLogicalOperators) {
// In the unlikely event that a Grouped Filter is in a non-SQS environment // In the unlikely event that a Grouped Filter is in a non-SQS environment
// It needs to be ignored. Entirely // It needs to be ignored entirely
let queryFilters: SearchFilter[] = Array.isArray(query) ? query : [] let queryFilters: SearchFilter[] = Array.isArray(view.query)
? view.query
: []
// Extract existing fields // Extract existing fields
const existingFields = const existingFields =
@ -64,15 +66,16 @@ export async function searchView(
Object.keys(body.query).forEach(key => { Object.keys(body.query).forEach(key => {
const operator = key as Exclude<SearchFilterKey, LogicalOperator> const operator = key as Exclude<SearchFilterKey, LogicalOperator>
Object.keys(body.query[operator] || {}).forEach(field => { Object.keys(body.query[operator] || {}).forEach(field => {
if (!existingFields.includes(db.removeKeyNumbering(field))) { if (query && !existingFields.includes(db.removeKeyNumbering(field))) {
query[operator]![field] = body.query[operator]![field] query[operator]![field] = body.query[operator]![field]
} }
}) })
}) })
} else { } else {
const conditions = query ? [query] : []
query = { query = {
$and: { $and: {
conditions: [query, body.query], conditions: [...conditions, body.query],
}, },
} }
} }
@ -80,7 +83,7 @@ export async function searchView(
await context.ensureSnippetContext(true) await context.ensureSnippetContext(true)
const enrichedQuery = await enrichSearchContext(query, { const enrichedQuery = await enrichSearchContext(query || {}, {
user: sdk.users.getUserContextBindings(ctx.user), user: sdk.users.getUserContextBindings(ctx.user),
}) })

View File

@ -428,8 +428,9 @@ const builderFilter = (expression: SearchFilter) => {
return query return query
} }
export const buildQueryLegacy = (filter: SearchFilter[]) => { export const buildQueryLegacy = (
// filter?: SearchFilterGroup | SearchFilter[]
): SearchFilters | undefined => {
let query: SearchFilters = { let query: SearchFilters = {
string: {}, string: {},
fuzzy: {}, fuzzy: {},
@ -523,7 +524,6 @@ export const buildQueryLegacy = (filter: SearchFilter[]) => {
} }
} }
} else if (isLogicalSearchOperator(queryOperator)) { } else if (isLogicalSearchOperator(queryOperator)) {
// TODO
} else if (query[queryOperator] && operator !== "onEmptyFilter") { } else if (query[queryOperator] && operator !== "onEmptyFilter") {
if (type === "boolean") { if (type === "boolean") {
// Transform boolean filters to cope with null. // Transform boolean filters to cope with null.
@ -548,7 +548,9 @@ export const buildQueryLegacy = (filter: SearchFilter[]) => {
return query return query
} }
export const buildQuery = (filter?: SearchFilterGroup | SearchFilter[]) => { export const buildQuery = (
filter?: SearchFilterGroup | SearchFilter[]
): SearchFilters | undefined => {
if (!filter) { if (!filter) {
return return
} }