lsc 8 月之前
父節點
當前提交
066f6a9352
共有 1 個文件被更改,包括 33 次插入31 次删除
  1. 33 31
      pbl.js

+ 33 - 31
pbl.js

@@ -47,43 +47,45 @@ async function calculateVectors(content) {
     }
 }
 
+
 async function findMatchingContents(discipline, content) {
-    const contentVector = await calculateVectors(content);
-    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
-
-    const client = await pool.connect();
-    try {
-        // 使用余弦相似度进行内容向量匹配
-        const query = `
-            SELECT id, content,
-                   1 - (content_vector <#> $1::vector) AS contentSimilarity
-            FROM (select id, content, content_vector from knowledgefiles as a where top_dir_id in (select id from directory_files where $2 = '' or folder_name = ANY(string_to_array($2, ',')))) as a
-            ORDER BY contentSimilarity DESC
-            LIMIT 5;
-        `;
-
-        const result = await client.query(query, [formattedContentVector, discipline]);
-        /*
-                console.log(result.rows);
-                // 将查询结果转换为匹配对象
-                const matches = result.rows.map(row => ({
-                    id: row.id,
-                    contentSimilarity: row.contentsimilarity
-                }));
-                console.log(matches);
-                */
-        return result.rows; // 返回匹配结果
-    } catch (error) {
-        console.error('Error finding matching contents:', error);
-        return [];
-    } finally {
-        client.release(); // 确保释放数据库连接
-    }
+    const contentVector = await calculateVectors(content);
+    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
+
+    const client = await pool.connect();
+    try {
+        // 使用余弦相似度进行内容向量匹配
+        const query = `
+            SELECT title, id, content,
+                   1 - (content_vector <#> $1::vector) AS contentSimilarity
+            FROM (select id, content, content_vector from knowledgefiles as a where top_dir_id in (select id from directory_files where $2 = '' or folder_name = ANY(string_to_array($2, ',')))) as a
+            ORDER BY contentSimilarity DESC
+            LIMIT 5;
+        `;
+
+        const result = await client.query(query, [formattedContentVector, discipline]);
+        /*
+                console.log(result.rows);
+                // 将查询结果转换为匹配对象
+                const matches = result.rows.map(row => ({
+                    id: row.id,
+                    contentSimilarity: row.contentsimilarity
+                }));
+                console.log(matches);
+                */
+        return result.rows; // 返回匹配结果
+    } catch (error) {
+        console.error('Error finding matching contents:', error);
+        return [];
+    } finally {
+        client.release(); // 确保释放数据库连接
+    }
 }
 
 /*检索文件*/
 router.route("/findMatchingContents").all(async (req, res, next) => {
     var json = queryString(req.url);
+    console.log(json["type"], json["string"])
     const titleMatches = await findMatchingContents(json["type"], json["string"]);
     res.end(JSON.stringify(titleMatches));
 });