in src/kg.py [0:0]
def fetch_entity_relations_with_keywords(conn, search_keyword, search_tags, search_topics):
# Convert the list of search keywords into a string suitable for SQL
if not search_keyword:
raise ValueError("search_keywords list cannot be empty.")
keyword_placeholder = f"'{search_keyword}'"
# print("keyword_placeholder = ", keyword_placeholder)
tag_placeholder = ', '.join(f"'{tag}'" for tag in search_tags)
# print("tag_placeholder = ", tag_placeholder)
topic_placeholder = ', '.join(f"'{topic}'" for topic in search_topics)
# print("topic_placeholder = ", topic_placeholder)
# Define the query with the dynamic IN clause
query = f"""
WITH entity_relations_info AS (
SELECT
m.entity,
m.entity_type,
m.relation,
m.url_hash,
m.score,
p.url,
p.title,
p.frecency
FROM
ml_kg_info m
JOIN
places_db.moz_places p
ON
m.url_hash = p.url_hash
WHERE
(m.entity IN ({keyword_placeholder}) AND
m.entity_type = 'keyword') OR
(m.entity IN ({tag_placeholder}) AND
m.entity_type = 'tag') OR
(m.entity IN ({topic_placeholder}) AND
m.entity_type = 'topic')
ORDER BY
m.score DESC
)
SELECT
url_hash,
url,
title,
SUM(score) AS total_score
FROM
entity_relations_info
GROUP BY
url_hash, url, title
ORDER BY
total_score DESC;
"""
results = pd.read_sql_query(query, conn)
return results