Browse Source

优化巴利单词搜索速度

visuddhinanda 4 years ago
parent
commit
63fe9a9a31
3 changed files with 73 additions and 3 deletions
  1. 56 0
      app/db/pali_text.php
  2. 2 0
      app/search/paliword.js
  3. 15 3
      app/search/paliword_sc.php

+ 56 - 0
app/db/pali_text.php

@@ -0,0 +1,56 @@
+<?php
+require_once "../path.php";
+require_once "../share/function.php";
+require_once "../db/table.php";
+
+class PaliText extends Table
+{
+    function __construct($redis=false) {
+		parent::__construct(_FILE_DB_PALITEXT_, "pali_text", "", "",$redis);
+    }
+
+	public function getTitle($book,$para)
+	{
+		if (isset($book) && isset($para)) {
+			if($this->redis!==false){
+				$title = $this->redis->hGet("para_title://pali","{$book}-{$para}");
+				if($title!==FALSE){
+					return $title;
+				}
+			}
+			$title="";
+			$query = "select * from pali_text where book = ? and paragraph = ?";
+			$stmt = $this->dbh->prepare($query);
+			$stmt->execute(array($book,$para));
+			$result = $stmt->fetch(PDO::FETCH_ASSOC);
+			if ($result) {
+				if($result["level"]>0 && $result["level"]<8){
+					$title= $result["toc"];
+				}
+				else{
+					$query = "select * from pali_text where book = ? and paragraph = ?";
+					$stmt = $this->dbh->prepare($query);
+					$stmt->execute(array($book,$result["parent"]));
+					$result = $stmt->fetch(PDO::FETCH_ASSOC);
+					if ($result) {
+						$title= $result["toc"];
+					}
+					else{
+						$title= "";
+					}
+				}
+				
+			} else {
+				$title= "";
+			}
+			$output = $this->redis->hSet("para_title://pali","{$book}-{$para}",$title);
+			return $title;
+		} else {
+			$title= "";
+		}
+	}
+
+
+}
+
+?>

+ 2 - 0
app/search/paliword.js

@@ -179,6 +179,8 @@ function render_nav(result) {
 			html += "<li >" + (index + 1) + "</li> ";
 		}
 	}
+	html += "<li >上一页</li> ";
+	html += "<li >下一页</li> ";
 	html += "</ul>";
 	return html;
 }

+ 15 - 3
app/search/paliword_sc.php

@@ -7,6 +7,10 @@ require_once "../public/_pdo.php";
 require_once "../public/load_lang.php"; //语言文件
 require_once "../public/function.php";
 require_once "../search/word_function.php";
+require_once "../db/pali_text.php";
+
+$_redis = redis_connect();
+$_dbPaliText = new PaliText($_redis);
 
 _load_book_index();
 
@@ -38,6 +42,7 @@ if (isset($_GET["page"])) {
 }
 
 if (count($arrWordList) > 1) {
+	# 查询多个词
     PDO_Connect(_FILE_DB_PALITEXT_);
     # 首先精确匹配
     $words = implode(" ", $arrWordList);
@@ -46,7 +51,7 @@ if (count($arrWordList) > 1) {
 
     foreach ($Fetch1 as $key => $value) {
         # code...
-        $newRecode["title"] = "title";
+        $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
         $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
         $newRecode["book"] = $value["book"];
         $newRecode["para"] = $value["paragraph"];
@@ -55,6 +60,8 @@ if (count($arrWordList) > 1) {
         $newRecode["wt"] = 0;
         $out_data[] = $newRecode;
     }
+	$result["time"][] = array("event" => "精确匹配结束", "time" => microtime(true)-$_start);
+	/*
     #然后查分散的
     $strQuery = "";
     foreach ($arrWordList as $oneword) {
@@ -67,7 +74,7 @@ if (count($arrWordList) > 1) {
 
     foreach ($Fetch2 as $key => $value) {
         # code...
-        $newRecode["title"] = "title";
+        $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
         $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
         $newRecode["book"] = $value["book"];
         $newRecode["para"] = $value["paragraph"];
@@ -76,7 +83,10 @@ if (count($arrWordList) > 1) {
         $newRecode["wt"] = 0;
         $out_data[] = $newRecode;
     }
-    $result["data"] = $out_data;
+    
+	$result["time"][] = array("event" => "查分散的结束", "time" => microtime(true)-$_start);
+*/
+	$result["data"] = $out_data;
     echo json_encode($result, JSON_UNESCAPED_UNICODE);
     # 然后查特别不精确的
     exit;
@@ -194,9 +204,11 @@ $result["time"][] = array("event" => "准备查询", "time" => microtime(true) -
 $time_start = microtime_float();
 
 PDO_Connect(_FILE_DB_PALI_INDEX_);
+
 $query = "SELECT count(*) from (SELECT book FROM word WHERE \"wordindex\" in $strQueryWordId  $strQueryBookId group by book,paragraph) where 1 ";
 $result["record_count"] = PDO_FetchOne($query);
 $result["time"][] = array("event" => "查询记录数", "time" => microtime(true) - $_start);
+
 $query = "SELECT book,paragraph, wordindex, sum(weight) as wt FROM word WHERE \"wordindex\" in $strQueryWordId $strQueryBookId GROUP BY book,paragraph ORDER BY wt DESC LIMIT 0,20";
 $Fetch = PDO_FetchAll($query);
 $result["time"][] = array("event" => "查询结束", "time" => microtime(true) - $_start);