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