lsc vor 7 Monaten
Ursprung
Commit
99db32054e
1 geänderte Dateien mit 35 neuen und 0 gelöschten Zeilen
  1. 35 0
      pbl.js

+ 35 - 0
pbl.js

@@ -110,6 +110,34 @@ async function findMatchingContentsPage2(discipline, content, grade, page) {
     }
 }
 
+
+async function findMatchingContentsPage3(discipline, content, grade, filetype, page) {
+    const contentVector = await calculateVectors(content);
+    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
+    const offset = ((page || 1) - 1) * 5;
+    const client = await pool.connect();
+    try {
+        // 使用余弦相似度进行内容向量匹配
+        const query = `
+            SELECT id, content, file_type,
+                   1 - (content_vector <#> $1::vector) AS contentSimilarity
+            FROM (select id, content, content_vector, file_type from knowledgefiles as a
+            where ($4 = '' or file_type = ANY(string_to_array($4, ','))) and ($2 = '' or a.parent_arr && (SELECT ARRAY_AGG(id)::text[] FROM directory_files WHERE folder_name = ANY(string_to_array($2, ',')))) 
+            AND ($3 = '' or a.parent_arr && (SELECT ARRAY_AGG(id)::text[] FROM directory_files WHERE folder_name = ANY(string_to_array($3, ','))))) as a
+            ORDER BY contentSimilarity DESC
+            LIMIT 5 OFFSET $5;
+        `;
+
+        const result = await client.query(query, [formattedContentVector, discipline, grade, filetype, offset]);
+        return result.rows; // 返回匹配结果
+    } catch (error) {
+        console.error('Error finding matching contents:', error);
+        return [];
+    } finally {
+        client.release(); // 确保释放数据库连接
+    }
+}
+
 async function findMatchingContentsPage(discipline, content, page) {
     const contentVector = await calculateVectors(content);
     const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
@@ -160,6 +188,13 @@ router.route("/findMatchingContentsPage2").all(async (req, res, next) => {
     res.end(JSON.stringify(titleMatches));
 });
 
+/*检索文件*/
+router.route("/findMatchingContentsPage3").all(async (req, res, next) => {
+    var json = queryString(req.url);
+    const titleMatches = await findMatchingContentsPage3(json["type"], json["string"], json["grade"], json["filetype"], json["page"]);
+    res.end(JSON.stringify(titleMatches));
+});
+
 
 //統壹處理區域
 router.use(async function(req, res, next) {