Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const NodeActionsMenu = ({ node }: { node: QueryNode }) => {
const [open, setOpen] = useState(false)

const handleToggleBoost = () => {
updateNode(node.id, { boost: node.boost ? undefined : 2 })
updateNode(node.id, { boost: node.boost === undefined ? 2 : undefined })
}

const handleToggleNot = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ const queries: (Record<string, unknown> | [Record<string, unknown>, Record<strin
{ $and: [{ $and: { name: "a", status: "b" } }, { $or: { active: true, age: 1 } }] },
{ $and: [{ name: "a", status: "b" }, { $or: { active: true, age: 1 } }] },
],

// boost 0 is kept, boost 1 is the default
{ name: { $smart: "foo", $boost: 0 } },
{ age: { $eq: 18, $boost: 0 } },
[{ name: { $smart: "foo", $boost: 1 } }, { name: "foo" }],
{ $or: { name: "a", status: "b", $boost: 0 } },
]

describe("query roundtrip: parse → stringify", () => {
Expand Down Expand Up @@ -325,6 +331,27 @@ const builderStates: [QueryNode, Record<string, unknown>][] = [
group("or", [cond("a", "1"), group("and", [cond("b", "2", true)])]),
{ $or: [{ a: { $eq: "1" } }, { $and: [{}], $mustNot: { b: { $eq: "2" } } }] },
],
[
group("or", [cond("a", "1"), cond("b", "2", true)], 0),
{ $or: [{ a: { $eq: "1" } }, { $mustNot: { b: { $eq: "2" } } }], $boost: 0 },
],
[
group("and", [cond("a", "1"), group("or", [cond("b", "2"), cond("c", "3")], 0)]),
{ $and: [{ a: { $eq: "1" } }, { $or: { b: { $eq: "2" }, c: { $eq: "3" } }, $boost: 0 }] },
],

// A boosted root keeps the boost of its only child group
[
group("and", [group("or", [cond("a", "1"), cond("b", "2", true)], -1)], 0.5),
{
$and: [{ $or: [{ a: { $eq: "1" } }, { $mustNot: { b: { $eq: "2" } } }], $boost: -1 }],
$boost: 0.5,
},
],
[
group("and", [group("and", [cond("a", "1"), cond("a", "2")], 3)], 2),
{ $and: [{ $and: [{ a: { $eq: "1" } }, { a: { $eq: "2" } }], $boost: 3 }], $boost: 2 },
],

// Empty groups add no constraint and are dropped
[group("or", [cond("a", "1"), group("and", [])]), { $or: { a: { $eq: "1" } } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { toJsLiteral } from "@/lib/utils"

import type { FieldOperator, QueryNode, QueryState } from "./types"

/** Boost 1 is the default and can be omitted, but 0 (or any other value) must be kept */
const hasBoost = (boost: number | undefined): boost is number => boost !== undefined && boost !== 1

/** A group with no conditions anywhere inside it adds no constraint, so it is skipped */
const isEmptyGroup = (node: QueryNode): boolean =>
node.type === "group" && node.children.every((child) => isEmptyGroup(child))
Expand Down Expand Up @@ -44,7 +47,7 @@ const conditionToObject = (node: QueryNode & { type: "condition" }): Record<stri
// Shorthand for $smart without boost, or $eq on boolean/number without boost
const isCollapsibleEq =
operator === "eq" && (typeof value === "boolean" || typeof value === "number")
if ((operator === "smart" || isCollapsibleEq) && !effectiveBoost) {
if ((operator === "smart" || isCollapsibleEq) && !hasBoost(effectiveBoost)) {
return { [field]: value }
}

Expand All @@ -54,9 +57,8 @@ const conditionToObject = (node: QueryNode & { type: "condition" }): Record<stri
}

// Apply boost to the field condition - node.boost takes priority over conditionBoost
const finalBoost = node.boost ?? conditionBoost
if (finalBoost && finalBoost !== 1) {
fieldCondition.$boost = finalBoost
if (hasBoost(effectiveBoost)) {
fieldCondition.$boost = effectiveBoost
}

return { [field]: fieldCondition }
Expand All @@ -77,7 +79,7 @@ const mergeConditions = (children: QueryNode[], boost?: number): Record<string,
Object.assign(merged, conditionToObject(child as QueryNode & { type: "condition" }))
}
}
if (boost && boost !== 1) {
if (hasBoost(boost)) {
merged.$boost = boost
}
return merged
Expand Down Expand Up @@ -123,7 +125,7 @@ const groupToObject = (
const withoutNot = { ...node, not: undefined } as QueryNode & { type: "group" }
const inner = groupToObject(withoutNot, false)
// OR group with not and no negated children → flatten to $mustNot array
if (groupOperator === "or" && !boost && negatedChildren.length === 0) {
if (groupOperator === "or" && !hasBoost(boost) && negatedChildren.length === 0) {
const childObjects = normalChildren.map((child) => queryNodeToObject(child, false))
return { $mustNot: childObjects }
}
Expand All @@ -139,18 +141,21 @@ const groupToObject = (
child.not ? { $mustNot: buildMustNot([child]) } : queryNodeToObject(child, false, true)
),
}
if (boost && boost !== 1) result.$boost = boost
if (hasBoost(boost)) result.$boost = boost
return result
}

// Optimization: flatten root AND children into top-level object
// Conditions become field keys, groups become $and/$or keys — if no key conflicts
if (isRoot && groupOperator === "and" && negatedChildren.length === 0) {
// Single child: just unwrap it
// Single child: just unwrap it, unless both have a boost (one $boost key can't hold both)
if (normalChildren.length === 1) {
const result = queryNodeToObject(normalChildren[0], false)
if (boost && boost !== 1) result.$boost = boost
return result
if (!hasBoost(boost)) return result
if (!("$boost" in result)) {
result.$boost = boost
return result
}
}

const conditionChildren = normalChildren.filter((c) => c.type === "condition")
Expand All @@ -161,7 +166,7 @@ const groupToObject = (
// Don't flatten if a group child has same operator, multiple children, and boost
// (would create confusing nested $and inside root AND)
const hasConflictingGroupChild = groupChildren.some(
(c) => c.groupOperator === groupOperator && c.children.length > 1 && c.boost
(c) => c.groupOperator === groupOperator && c.children.length > 1 && hasBoost(c.boost)
)

if (!hasConflictingGroupChild) {
Expand All @@ -182,7 +187,7 @@ const groupToObject = (

if (canFlatten) {
const result: Record<string, unknown> = Object.assign({}, ...childObjects)
if (boost && boost !== 1) result.$boost = boost
if (hasBoost(boost)) result.$boost = boost
return result
}
}
Expand All @@ -199,7 +204,7 @@ const groupToObject = (
if (forArray) {
// Array context: boost goes outside as sibling of $op key
result[`$${groupOperator}`] = mergeConditions(normalChildren)
if (boost && boost !== 1) result.$boost = boost
if (hasBoost(boost)) result.$boost = boost
} else {
// Standalone/merged context: boost goes inside the operator value
result[`$${groupOperator}`] = mergeConditions(normalChildren, boost)
Expand Down Expand Up @@ -246,7 +251,7 @@ const groupToObject = (
result.$mustNot = buildMustNot(negatedChildren)
}

if (boost && boost !== 1) {
if (hasBoost(boost)) {
result.$boost = boost
}

Expand Down
Loading