fix (postgres query): fixed metadata filtering for postgres pg queries

This commit is contained in:
GGrassia 2025-10-08 18:23:01 +02:00
parent 177ec23821
commit f4c2823c82

View file

@ -4907,54 +4907,58 @@ SQL_TEMPLATES = {
file_path=EXCLUDED.file_path, file_path=EXCLUDED.file_path,
update_time = EXCLUDED.update_time update_time = EXCLUDED.update_time
""", """,
"relationships": """ "relationships": """
WITH top_relationships AS ( WITH filtered_chunks AS (
-- Step 1: Select only the chunk IDs that match the metadata filter
SELECT SELECT
r.id, c.id
r.source_id, FROM
r.target_id, LIGHTRAG_VDB_CHUNKS c
r.create_time, WHERE
r.metadata, c.workspace = $1
r.chunk_ids {metadata_filter_clause}
)
-- Step 2 & 3: Join relationships with the filtered chunks and rank by similarity
SELECT
r.source_id AS src_id,
r.target_id AS tgt_id,
EXTRACT(EPOCH FROM r.create_time)::BIGINT AS created_at
FROM FROM
LIGHTRAG_VDB_RELATION r LIGHTRAG_VDB_RELATION r
JOIN
filtered_chunks fc ON r.chunk_ids && ARRAY[fc.id] -- Find relationships linked to our valid chunks
WHERE WHERE
r.workspace = $1 r.workspace = $1
-- Apply the metadata filter here to reduce the set of relationships searched AND r.content_vector <=> '[{embedding_string}]'::vector < $2
{metadata_filter_clause}
ORDER BY ORDER BY
r.content_vector <=> '[{embedding_string}]'::vector r.content_vector <=> '[{embedding_string}]'::vector -- Rank the resulting relationships
LIMIT $3 LIMIT $3;
)
SELECT
tr.source_id AS src_id,
tr.target_id AS tgt_id,
EXTRACT(EPOCH FROM tr.create_time)::BIGINT AS created_at
FROM
top_relationships tr
JOIN LIGHTRAG_VDB_CHUNKS c ON tr.chunk_ids && ARRAY[c.id];
""", """,
"entities": """ "entities": """
WITH top_entities AS ( WITH filtered_chunks AS (
-- Step 1: Select only the chunk IDs that match the metadata filter
SELECT
c.id
FROM
LIGHTRAG_VDB_CHUNKS c
WHERE
c.workspace = $1
{metadata_filter_clause}
)
-- Step 2 & 3: Join entities with the filtered chunks and rank by similarity
SELECT SELECT
e.id,
e.entity_name, e.entity_name,
e.create_time, EXTRACT(EPOCH FROM e.create_time)::BIGINT AS created_at
e.metadata, FROM
e.chunk_ids LIGHTRAG_VDB_ENTITY e
FROM LIGHTRAG_VDB_ENTITY e JOIN
filtered_chunks fc ON e.chunk_ids && ARRAY[fc.id]
WHERE WHERE
e.workspace = $1 e.workspace = $1
{metadata_filter_clause} AND e.content_vector <=> '[{embedding_string}]'::vector < $2
ORDER BY ORDER BY
e.content_vector <=> '[{embedding_string}]'::vector e.content_vector <=> '[{embedding_string}]'::vector
LIMIT $3) LIMIT $3;
SELECT
te.entity_name,
EXTRACT(EPOCH FROM te.create_time)::BIGINT AS created_at
FROM
top_entities te
JOIN LIGHTRAG_VDB_CHUNKS c ON te.chunk_ids && ARRAY[c.id];
""", """,
"chunks": """ "chunks": """
SELECT c.id, SELECT c.id,
@ -4968,6 +4972,7 @@ SQL_TEMPLATES = {
WHERE WHERE
c.workspace = $1 c.workspace = $1
{metadata_filter_clause} {metadata_filter_clause}
AND c.content_vector <=> '[{embedding_string}]'::vector < $2
ORDER BY ORDER BY
c.content_vector <=> '[{embedding_string}]'::vector c.content_vector <=> '[{embedding_string}]'::vector
LIMIT $3; LIMIT $3;