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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions queries/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
emb.txt
semantic.txt
31 changes: 31 additions & 0 deletions queries/01-fulltext.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Full-text/BM25 search.
-- Required: schema, user_id, fulltext. Optional: limit, query_prefix.
-- Mirrors buildBM25Query without additional filters.

\ir _setup.sql

\if :{?fulltext}
\else
\prompt 'fulltext: ' fulltext
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, -(content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx')) as score
from :"schema".memory
where content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx') < 0
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
28 changes: 28 additions & 0 deletions queries/02-semantic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Semantic/HNSW search.
-- Required: schema, user_id. Provide emb or semantic. Optional: limit, query_prefix.
-- Mirrors buildSemanticQuery without additional filters and without semanticThreshold.

\ir _setup.sql
\ir _embedding.sql

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, (1 - (embedding <=> :'emb'::halfvec)) as score
from :"schema".memory
where embedding is not null
and (embedding <=> :'emb'::halfvec) < 1.0
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
31 changes: 31 additions & 0 deletions queries/03-ltree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Filter-only ltree subtree query.
-- Required: schema, user_id, tree. Optional: limit, order_direction, query_prefix.
-- Mirrors buildFilterQuery with a plain ltree filter.

\ir _setup.sql

\if :{?tree}
\else
\prompt 'tree: ' tree
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, 1.0 as score
from :"schema".memory
where tree <@ :'tree'::ltree
order by created_at :order_direction
limit :limit;

\ir _teardown.sql
31 changes: 31 additions & 0 deletions queries/04-meta.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Filter-only JSONB metadata containment query.
-- Required: schema, user_id, meta. Optional: limit, order_direction, query_prefix.
-- Mirrors buildFilterQuery with meta @> filter.

\ir _setup.sql

\if :{?meta}
\else
\prompt 'meta json: ' meta
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, 1.0 as score
from :"schema".memory
where meta @> :'meta'::jsonb
order by created_at :order_direction
limit :limit;

\ir _teardown.sql
37 changes: 37 additions & 0 deletions queries/05-meta-ltree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Filter-only JSONB metadata + ltree subtree query.
-- Required: schema, user_id, meta, tree. Optional: limit, order_direction, query_prefix.
-- Mirrors buildFilterQuery with common filters in app order: meta, tree.

\ir _setup.sql

\if :{?meta}
\else
\prompt 'meta json: ' meta
\endif

\if :{?tree}
\else
\prompt 'tree: ' tree
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, 1.0 as score
from :"schema".memory
where meta @> :'meta'::jsonb
and tree <@ :'tree'::ltree
order by created_at :order_direction
limit :limit;

\ir _teardown.sql
37 changes: 37 additions & 0 deletions queries/06-fulltext-ltree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Full-text/BM25 search with ltree subtree filter.
-- Required: schema, user_id, fulltext, tree. Optional: limit, query_prefix.
-- Mirrors buildBM25Query with common filters in app order: tree.

\ir _setup.sql

\if :{?fulltext}
\else
\prompt 'fulltext: ' fulltext
\endif

\if :{?tree}
\else
\prompt 'tree: ' tree
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, -(content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx')) as score
from :"schema".memory
where content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx') < 0
and tree <@ :'tree'::ltree
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
37 changes: 37 additions & 0 deletions queries/07-fulltext-meta.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Full-text/BM25 search with JSONB metadata filter.
-- Required: schema, user_id, fulltext, meta. Optional: limit, query_prefix.
-- Mirrors buildBM25Query with common filters in app order: meta.

\ir _setup.sql

\if :{?fulltext}
\else
\prompt 'fulltext: ' fulltext
\endif

\if :{?meta}
\else
\prompt 'meta json: ' meta
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, -(content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx')) as score
from :"schema".memory
where content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx') < 0
and meta @> :'meta'::jsonb
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
43 changes: 43 additions & 0 deletions queries/08-fulltext-meta-ltree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Full-text/BM25 search with JSONB metadata + ltree subtree filters.
-- Required: schema, user_id, fulltext, meta, tree. Optional: limit, query_prefix.
-- Mirrors buildBM25Query with common filters in app order: meta, tree.

\ir _setup.sql

\if :{?fulltext}
\else
\prompt 'fulltext: ' fulltext
\endif

\if :{?meta}
\else
\prompt 'meta json: ' meta
\endif

\if :{?tree}
\else
\prompt 'tree: ' tree
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, -(content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx')) as score
from :"schema".memory
where content <@> to_bm25query(:'fulltext', :'schema' || '.memory_content_bm25_idx') < 0
and meta @> :'meta'::jsonb
and tree <@ :'tree'::ltree
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
34 changes: 34 additions & 0 deletions queries/09-semantic-ltree.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Semantic/HNSW search with ltree subtree filter.
-- Required: schema, user_id, tree. Provide emb or semantic. Optional: limit, query_prefix.
-- Mirrors buildSemanticQuery with common filters in app order: tree.

\ir _setup.sql
\ir _embedding.sql

\if :{?tree}
\else
\prompt 'tree: ' tree
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, (1 - (embedding <=> :'emb'::halfvec)) as score
from :"schema".memory
where embedding is not null
and (embedding <=> :'emb'::halfvec) < 1.0
and tree <@ :'tree'::ltree
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
34 changes: 34 additions & 0 deletions queries/10-semantic-meta.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Semantic/HNSW search with JSONB metadata filter.
-- Required: schema, user_id, meta. Provide emb or semantic. Optional: limit, query_prefix.
-- Mirrors buildSemanticQuery with common filters in app order: meta.

\ir _setup.sql
\ir _embedding.sql

\if :{?meta}
\else
\prompt 'meta json: ' meta
\endif

\timing on

:query_prefix
select
id
, content
, meta
, tree::text
, temporal::text
, embedding is not null as has_embedding
, created_at
, created_by
, updated_at
, (1 - (embedding <=> :'emb'::halfvec)) as score
from :"schema".memory
where embedding is not null
and (embedding <=> :'emb'::halfvec) < 1.0
and meta @> :'meta'::jsonb
order by score desc, created_at desc
limit :limit;

\ir _teardown.sql
Loading