Skip to content

feat(search): filter by artifact property (prop:KEY=VALUE) - #466

Merged
M0Rf30 merged 1 commit into
openartifact:mainfrom
orcamedia:feat/search-by-property
Nov 11, 2026
Merged

M0Rf30 merged 1 commit into
openartifact:mainfrom
orcamedia:feat/search-by-property

Conversation

@orcamedia

Copy link
Copy Markdown
Contributor

Closes #459, with your answers: both forms, q optional with prop, at most 10 filters, no prop: in the too-broad hint, docker left out.

API

  • prop:KEY=VALUE in q, and the repeatable prop parameter (prop=client%3DACME%20Corp) for values with spaces. Split at the first =, exact match on key and value, all filters must hold.
  • q may be left out when a prop filter is given, as with flagged=true.
  • A filter matches the id the web UI and oa store properties under: a live package_files row of the same repository (a property stored in one repository under another repository's file id doesn't match). Docker rows are excluded (s.format <> 'docker' drops the branch); I didn't confirm their id mapping, so they're left for a follow-up.
  • No repository hits with prop, as with sha256: and flagged.
  • search.CheckPropFilters (key 1 to 255 bytes, value up to 4096, valid UTF-8, no control characters in the key, no NUL in the value, at most 10) is shared by the handler (400 invalid_filter) and search_artifacts (tool error, before any query).

The plan check, and why there are two shapes

You asked for the plan with q omitted on a seeded database. I measured with EXPLAIN ANALYZE on the scope seed plus one repository of 300,000 files (seeded with triggers off: the per-row triggers of package_files make a bulk insert crawl), with status=draft on every file, status=approved on 25, and channel=beta on about 5,000:

filter semi-join (repo, id) IN (…) id array, PK probes correlated EXISTS
status=approved (25) 49 ms (reads every file) 1.3 ms not measured (probes every file)
channel=beta (~5k) 103 ms 59 ms
status=draft (300k) 853 ms (sort + unique of all matches) 1.6 s 49 ms (stops at the cap)
status=draft + name:f777 3.8 ms 0.5 ms

Two findings decided the shape:

  • Postgres can't push a join into the UNION ALL search source, so a semi-join always reads every file. For few matches the fix is to compute the matching file ids first, as an array, which is pushed into each branch as f.id = ANY(…) and probes package_files_pkey.
  • For many matches, a per-row EXISTS is better: the capped search stops early, and with a selective q it only probes the rows that are left. The semi-join instead collects and de-duplicates every match first.

So Executor.planProps counts each filter's properties (bounded, LIMIT 1001) and applies it by id array up to 1000 matches, as EXISTS above. Both shapes select the same rows.

Index: 029_artifact_properties_key_value.no_tx.sql adds (key, value) INCLUDE (artifact_ref_id), built CONCURRENTLY with the invalid-index guard of 021. It serves the id array (index-only, 25 rows instead of every status row through (repository_id, key)) and the bounded count. The (artifact_ref_id, key, value) index you mentioned turned out not to be needed: the EXISTS probe is fast with artifact_refs (repository_id, artifact_path) and idx_artifact_properties_ref_id, so I didn't add a second index.

Tests

  • prop_filter_integration_test.go: every case runs in both shapes with the same expected rows: q omitted (readable repositories only, the cross-repository id doesn't match), with text, two filters, a value with a space, exact match (no prefix), no match, with format, and group=package returning the latest matching version. TestPlanPropsPicksTheShape (rare → id array, more than 1000 → EXISTS), TestPropFilterPlans (enable_seqscan = off like TestVulnScanOverviewUsesIndexes: the id array uses idx_artifact_properties_key_value and package_files_pkey; EXISTS is a nested-loop semi-join, never a hash semi-join over all matches), and no repository hits with prop.
  • Unit: the qualifier, CheckPropFilters (limits, = in values), the handler (parameter with a space, both forms, q omitted, 400s), and search_artifacts (passes prop: on, refuses malformed and an 11th filter before searching).

Also

Checked locally (macOS, Postgres 18): make check, cmd/docgen -check, bun run typecheck, the unit suite with -race, the slim suite, go test -tags integration ./internal/search/, and the migration tests.

🤖 Generated with Claude Code

GET /api/v1/search and the MCP search_artifacts tool keep the artifacts
carrying a property: prop:KEY=VALUE in q, or the repeatable prop parameter
for values with spaces. Exact match, all filters must hold, at most 10;
q may be left out, as with flagged=true. A filter matches the id the web UI
and oa store properties under (a package_files row of the same repository);
docker images are left out for now. With prop no repositories are returned.
Malformed filters are 400 invalid_filter (a tool error on MCP).

Each filter is applied in the shape that fits how many properties match,
decided by a bounded count: up to 1000 by an id array pushed into the
search source, so package_files is probed by primary key (Postgres cannot
push a join into the UNION ALL); more as a correlated EXISTS, which the
capped search can stop early. Migration 029 adds
idx_artifact_properties_key_value (key, value) INCLUDE (artifact_ref_id),
built concurrently.

The upgrade notes get a new "Unreleased" section with this and the oa
upload content-type change of openartifact#460, which was filed under 0.23.0 although
it merged after that release.

Closes openartifact#459

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@M0Rf30
M0Rf30 merged commit da3f71e into openartifact:main Nov 11, 2026
23 checks passed
@M0Rf30

M0Rf30 commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search: filter by artifact property (prop:KEY=VALUE)

2 participants