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
18 changes: 16 additions & 2 deletions frontend/src/lib/searchParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
const SINGLE_VALUE_TAGS = ['status', 'type', 'category', 'from', 'assigned', 'reviewed', 'sort', 'confidence', 'template', 'proposal', 'mission'];
const MULTI_VALUE_TAGS = ['exclude', 'include', 'has', 'no', 'is', 'not'];
const NUMERIC_TAGS = ['min-contributions'];
const NEGATED_MULTI_VALUE_TAGS = {
exclude: 'include',
include: 'exclude',
has: 'no',
no: 'has',
is: 'not',
not: 'is'
};

/**
* Tokenize the search query, respecting quoted strings.
Expand Down Expand Up @@ -130,29 +138,35 @@ export function parseSearch(query) {
}

const tokens = tokenize(query);
let negateNext = false;

for (const token of tokens) {
// Handle "NOT tag:value" as two tokens
if (token.toUpperCase() === 'NOT') {
negateNext = true;
continue; // Will be handled with next token
}

const parsed = parseToken(token);
if (!parsed) {
// Untagged text — collect as free-text search terms
filters.freeText.push(token);
negateNext = false;
continue;
}

const { tag, value, negated } = parsed;
const { tag, value } = parsed;
const negated = parsed.negated || negateNext;
negateNext = false;

// Handle single-value tags
if (SINGLE_VALUE_TAGS.includes(tag)) {
filters[tag] = { value, negated };
}
// Handle multi-value tags
else if (MULTI_VALUE_TAGS.includes(tag)) {
filters[tag].push(value);
const targetTag = negated ? NEGATED_MULTI_VALUE_TAGS[tag] : tag;
filters[targetTag].push(value);
}
// Handle numeric tags
else if (NUMERIC_TAGS.includes(tag)) {
Expand Down
49 changes: 49 additions & 0 deletions frontend/src/tests/searchParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect, it } from "vitest";
import { parseSearch } from "../lib/searchParser.js";
import { searchToParams } from "../lib/searchToParams.js";

function paramsFor(query) {
return searchToParams(parseSearch(query));
}

describe("steward search negation", () => {
it("normalizes negated presence filters to absence filters", () => {
const { filters } = parseSearch("-has:proposal -has:url -has:appeal");

expect(filters.has).toEqual([]);
expect(filters.no).toEqual(["proposal", "url", "appeal"]);
expect(paramsFor("-has:proposal")).toEqual({ has_proposal: false });
expect(paramsFor("-has:url")).toEqual({ only_empty_evidence: true });
expect(paramsFor("-has:appeal")).toEqual({ has_appeal: false });
});

it("normalizes negated flag filters to not filters", () => {
const { filters } = parseSearch("-is:interesting -is:resubmitted");

expect(filters.is).toEqual([]);
expect(filters.not).toEqual(["interesting", "resubmitted"]);
expect(paramsFor("-is:interesting")).toEqual({ is_interesting: false });
expect(paramsFor("-is:resubmitted")).toEqual({ resubmitted_more_info: false });
});

it("supports NOT before multi-value filters", () => {
expect(paramsFor("NOT has:proposal")).toEqual({ has_proposal: false });
expect(paramsFor("NOT is:interesting")).toEqual({ is_interesting: false });
});

it("inverts explicit negative aliases when prefixed with a dash", () => {
expect(paramsFor("-no:proposal")).toEqual({ has_proposal: true });
expect(paramsFor("-not:interesting")).toEqual({ is_interesting: true });
});

it("normalizes negated include and exclude text filters", () => {
const { filters } = parseSearch("-include:spam -exclude:genlayer");

expect(filters.include).toEqual(["genlayer"]);
expect(filters.exclude).toEqual(["spam"]);
expect(paramsFor("-include:spam -exclude:genlayer")).toEqual({
exclude_content: "spam",
include_content: "genlayer",
});
});
});