lsc 7 ماه پیش
والد
کامیت
be1c8b6d4f
1فایلهای تغییر یافته به همراه34 افزوده شده و 0 حذف شده
  1. 34 0
      pbl.js

+ 34 - 0
pbl.js

@@ -82,6 +82,31 @@ async function findMatchingContents(discipline, content) {
     }
 }
 
+async function findMatchingContentsPage(discipline, content, 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,
+                   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 OFFSET $3;
+        `;
+
+        const result = await client.query(query, [formattedContentVector, discipline, offset]);
+        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);
@@ -90,6 +115,15 @@ router.route("/findMatchingContents").all(async (req, res, next) => {
     res.end(JSON.stringify(titleMatches));
 });
 
+/*检索文件*/
+router.route("/findMatchingContentsPage").all(async (req, res, next) => {
+    var json = queryString(req.url);
+    console.log(json["type"], json["string"])
+    const titleMatches = await findMatchingContentsPage(json["type"], json["string"], json["page"]);
+    res.end(JSON.stringify(titleMatches));
+});
+
+
 //統壹處理區域
 router.use(async function(req, res, next) {
     await asynnext(req, res, next);