Skip to content

Commit 029c07b

Browse files
author
zho
committed
#15-fix to process and, or operator
1 parent 6c5269d commit 029c07b

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/server/metadata-filter.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const metadataFilterValueSchema: z.ZodType<unknown> = z.lazy(() =>
99
z.boolean(),
1010
z.array(z.string()),
1111
z.array(z.number()),
12+
z.array(z.lazy(() => metadataFilterSchema)),
1213
z.record(z.string(), metadataFilterValueSchema), // Recursive for nested operators
1314
])
1415
);
@@ -23,6 +24,8 @@ const ALLOWED_FILTER_OPERATORS = new Set([
2324
'$lte',
2425
'$in',
2526
'$nin',
27+
'$and',
28+
'$or',
2629
]);
2730

2831
/** True if value is a string, number, or boolean (allowed for $eq, $gt, etc.). */
@@ -45,7 +48,17 @@ function validateMetadataFilterValue(value: unknown, path: string[]): string | n
4548
return null;
4649
}
4750

48-
if (typeof value !== 'object' || Array.isArray(value)) {
51+
if (Array.isArray(value)) {
52+
for (let i = 0; i < value.length; i++) {
53+
const parsed = metadataFilterSchema.safeParse(value[i]);
54+
if (!parsed.success) {
55+
return `Operator "${path[path.length - 1]}" at "${path.join('.')}" must use an array of filter objects.`;
56+
}
57+
}
58+
return null;
59+
}
60+
61+
if (typeof value !== 'object') {
4962
return `Unsupported filter value at "${path.join('.')}".`;
5063
}
5164

@@ -70,6 +83,9 @@ function validateMetadataFilterValue(value: unknown, path: string[]): string | n
7083
) {
7184
return `Operator "${key}" at "${path.join('.')}" must use a primitive value.`;
7285
}
86+
if ((key === '$and' || key === '$or') && !Array.isArray(nestedValue)) {
87+
return `Operator "${key}" at "${path.join('.')}" must use an array of filter objects.`;
88+
}
7389

7490
const nestedError = validateMetadataFilterValue(nestedValue, [...path, key]);
7591
if (nestedError) {

src/server/namespace-router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NamespaceInfo } from './namespaces-cache.js';
22

3-
type RankedNamespace = {
3+
export type RankedNamespace = {
44
namespace: string;
55
score: number;
66
record_count: number;
@@ -28,7 +28,7 @@ function scoreNamespace(
2828
score += 3;
2929
reasons.push('query mentions namespace name');
3030
} else {
31-
const nameTokens = normalizedName.split(/\s+/).filter(Boolean);
31+
const nameTokens = [...new Set(normalizedName.split(/\s+/).filter(Boolean))];
3232
for (const token of nameTokens) {
3333
if (token.length >= 2 && q.includes(token)) {
3434
score += 2;
@@ -56,7 +56,7 @@ export function rankNamespacesByQuery(
5656
namespaces: NamespaceInfo[],
5757
topN: number
5858
): RankedNamespace[] {
59-
const limit = Math.max(1, Math.floor(topN));
59+
const limit = Number.isFinite(topN) ? Math.max(1, Math.floor(topN)) : 1;
6060
return namespaces
6161
.map((ns) => {
6262
const fields = Object.keys(ns.metadata ?? {});

0 commit comments

Comments
 (0)