Hongmu Notes
Home Program Notes Empire cms related search call optimization
Program Notes Empire cms

Empire cms related search call optimization

Empire cms related search call optimization

In Empire, I have a new idea. Add a table to store the keywords of all articles and aggregate the IDs of related articles together, similar to a search aggregation effect.

The advantage of this is that you can call articles with specified keywords in the article, and you can put aside the related search aggregation function that Empire originally had!

Because the original related search aggregation function of Empire was too slow! (Using SQL fuzzy query, the data is more than 100,000. If the server is stretched a little, it will be uncomfortable!)

1. Create the corresponding keyword data table


CREATE TABLE `phome_keywords` ( `id` INT(10) NOT NULL AUTO_INCREMENT , `md5` CHAR(32) NOT NULL , `title` VARCHAR(100) NOT NULL , `keyid` TEXT NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;

2. Assign all article keywords to this table, create a PHP file, and write code

The first step: export all the keywords, I use the code... Actually exporting the database directly is the fastest



<?php
echo '<meta http-equiv="refresh" content="2"/>';        // 自动跳转
require('../e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('../e/class/db_sql.php');        //引入数据库操作文件
$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类

// 参数配置
$num = 10000;     //每次修改多少条数据
// 获取页数
$file = "ktabe.txt";
if(file_exists($file)){
    $page_num = file_get_contents($file);
}else{
    $page_num = 0;
}
$page_id = $page_num*$num;
$sql=$empire->query("select id,keyboard from {$dbtbpre}ecms_news where `keyboard` != '22' or `keyboard` != ''  limit {$page_id},{$num}"); 
//查询新闻表最新10条记录
while($r=$empire->fetch($sql))        //循环获取查询记录
{
if(empty($r['id'])){
    exit;
}
echo $r['id'].'<br>';
$array = explode(",",$r['keyboard']);
foreach ($array as $v) {
    file_put_contents("ktabe_cache.txt",$v."\n",FILE_APPEND);
}
$array = [];

}

$page_num++;
file_put_contents($file,$page_num);     //记录下次需要查询的页面

db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量
?>

The second step is to import the organized keywords into the data table



<?php
// echo '<meta http-equiv="refresh" content="2"/>';        // 自动跳转
require('../e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('../e/class/db_sql.php');        //引入数据库操作文件
$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类

$file = "ktabe_cache.txt";
$content = file_get_contents($file);
$array = explode(PHP_EOL,$content);
// print_r($array);
$i=1;
foreach ($array as $k=>$v){
    $v = stripslashes($v);
    $sql.= "(null,'".md5($v)."', '{$v}', ''),";
    // echo $sql;
    if($i>=100){
        $sql = rtrim($sql,',');
        $empire->query("INSERT INTO `{$dbtbpre}keywords` (`id`,`md5`, `title`, `keyid`) VALUES $sql;");
        $sql = '';
        $i=1;
    }
    $i++;
}
$sql = rtrim($sql,',');
$empire->query("INSERT INTO `{$dbtbpre}keywords` (`id`,`md5`, `title`, `keyid`) VALUES $sql;");

db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量
?>


The third step is to remove duplicates



<?php

require('../e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('../e/class/db_sql.php');        //引入数据库操作文件
$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类
$sql=$empire->query("SELECT id,md5, count( md5 ) FROM `phome_keywords` GROUP BY md5 HAVING count( md5 ) > 1 limit 10000");
if(empty($sql)){
    die;
}else{
    echo '<meta http-equiv="refresh" content="2"/>';
}
while($r=$empire->fetch($sql))        //循环获取查询记录
{
    $empire->query("DELETE FROM `phome_keywords` WHERE `id` = {$r['id']};\n");
    echo $r['id']."  ";
}

echo "两秒后进行第二次删除";

db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量
?>

After the code is written, the article keywords in the news table can be divided into the keyword data table one by one after running!

SQL mainly used for deduplication:


SELECT id, md5, count( md5 ) FROM `phome_keywords` GROUP BY md5 HAVING count( md5 ) > 1

3. Aggregate and store all article IDs with keywords in the keyword table

Since the SQL like of the database is too slow and stuck, I use sphinx.

PHP aggregation code:


<?php
echo '<meta http-equiv="refresh" content="1"/>';
require('../e/class/connect.php');        //引入数据库配置文件和公共函数文件
require('../e/class/db_sql.php');        //引入数据库操作文件
require ( "sphinxapi.php" );            //引入sphinx api文件
//配置sphinx
$cl = new SphinxClient ();
$host = "127.0.0.1";
$port = 9312;
$index = "article";
$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout ( 1 );
$cl->SetArrayResult ( true );
$cl->SetLimits(0, 10, 1000);
//翻页配置
$file = "search.txt";
$num = 1000;
if(file_exists($file)){
    $page_num = file_get_contents($file);
}else{
    $page_num = 0;
}
$page_id = $page_num*$num;

$link=db_connect();                //连接MYSQL
$empire=new mysqlquery();        //声明数据库操作类
$sql=$empire->query("SELECT id,title FROM `phome_keywords` ORDER BY `id` ASC  limit {$page_id},{$num}");
while($r=$empire->fetch($sql))        //循环获取查询记录
{
    $res = $cl->Query ( $r['title'], $index );
    // print_r($res);
    if($res['total'] > 2){
        foreach ($res['matches'] as $v1){
            $sql_data.= $v1['id'] . ",";
        }
        $sql_data =rtrim($sql_data,",");
        if(!empty($sql_data)){
            $empire->query("UPDATE `phome_keywords` SET `keyid` = '{$sql_data}' WHERE `phome_keywords`.`id` = {$r['id']};");
        }
    }
    echo "本次聚合id".$r['id']."<br>";
}
$page_num++;
file_put_contents($file,$page_num);
db_close();                        //关闭MYSQL链接
$empire=null;                        //注消操作类变量
?>

Done! If you call it now, it will be more relevant!

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Empire CMS database statement & SQL statement format

Empire CMS database statement & SQL statement format Program Notes Empire cms

Introduction to Imperial CMS extended SQL program writing, basic examples of Imperial CMS database statements & SQL statement formats: Note: The following examples are based on placing PHP files in the system root directory. Example 1: Connect to MYSQL program. (a.php)<?php require('e/class/connect.php'); //Introduce database configuration files and public function files requ…
👁 351
Empire CMS obtains the system COOKIE variable function getcvar()

Empire CMS obtains the system COOKIE variable function getcvar() Program Notes Empire cms

Get the system COOKIE variable function syntax: getcvar($var,$ecms) Description: $var: is the variable name $ecms: 0 is to set the foreground COOKIE variable, 1 is to set the background COOKIE variable. This parameter can be omitted and defaults to 0. Usage example: getcvar('mlusername'), get the user name of the front-end login member getcvar('loginu...
👁 296
Empire CMS smart label call field collection

Empire CMS smart label call field collection Program Notes Empire cms

Collect and classify all fields that support smart tag calls into Empire CMS smart tags: [e:loop={column ID/topic ID, number of items displayed, operation type, only display pictures with titles, additional SQL conditions, display sorting}] Template code content [/e:loop] Calling time: <?=date('m-d',$bqr[newstime])?> <?=dat…
👁 3749

Recommended reading

(PC + WAP) Psychological Counseling Website Template – Download Source Code for Family and Marriage Counseling Agency Websites – 0988

(PC + WAP) Psychological Counseling Website Template – Download Source Code for Family and Marriage Counseling Agency Websites – 0988 Practical Collection pbootcms Template

A PbootCMS website template designed for psychological counseling and family/marital counseling agencies, compatible with both PC and WAP devices. Its professional yet welcoming design is ideal for showcasing service offerings and counselor teams. This template helps counseling agencies build a professional online presence and attract clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 48
Why may fever or chills occur after taking Guizhi Decoction?

Why may fever or chills occur after taking Guizhi Decoction? Hobbies Memo notes

Guizhi Decoction: The primary efficacy of Guizhi Decoction lies in the diverse pharmacological actions of its ingredients, which enable it to treat a variety of conditions. Firstly, the cinnamon twig (Guizhi) in this decoction helps warm and activate Yang energy, support the spleen and stomach, release the exterior and induce sweating, as well as dispel cold and alleviate pain. It enhances the vitality of the body's Yang energy, promotes metabolism, strengthens the immune system, and helps prevent colds, malaria, and other febrile or cold-related illnesses. Secondly, the peony root (Shaoyao) in Guizhi Decoction possesses the property of promoting blood circulation and resolving blood stasis; it can improve blood flow and relieve symptoms such as dysmenorrhea and irregular menstruation....
👁 2142
(PC + WAP) Interior Design Website Template – Download Website Source Code for Decoration Companies – 0989

(PC + WAP) Interior Design Website Template – Download Website Source Code for Decoration Companies – 0989 Practical Collection pbootcms Template

A PbootCMS website template designed for interior design and renovation companies, compatible with both PC and WAP devices. Its modern, professional design is ideal for showcasing renovation project examples, construction expertise, and service workflows. This template helps renovation companies attract homeowners online and secure new projects. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: P...
👁 55
In Traditional Chinese Medicine, when pathogenic factors invade the body – specifically during the Jueyin, Shaoyin, Taiyin, Shaoyang, Yangming, or Taiyang phases – what are the pathways through which these pathogenic factors enter the body, and what are the corresponding clinical manifestations?

In Traditional Chinese Medicine, when pathogenic factors invade the body – specifically during the Jueyin, Shaoyin, Taiyin, Shaoyang, Yangming, or Taiyang phases – what are the pathways through which these pathogenic factors enter the body, and what are the corresponding clinical manifestations? Hobbies Memo notes

According to Traditional Chinese Medicine (TCM) theory, the normal functioning of the human body depends on maintaining the balance of Zheng Qi. Zheng Qi primarily circulates through the six meridians: Jueyin, Shaoyin, Taiyin, Shaoyang, Yangming, and Taiyang. When the body is invaded by pathogenic factors, TCM theory posits that these factors will enter different meridians depending on their nature and direction, thereby giving rise to corresponding clinical manifestations. Below are the pathways through which pathogenic factors invade the various meridians and the corresponding physical symptoms: Jueyin Meridian: When pathogenic factors enter through the sole of the foot, they may cause symptoms such as dry mouth, cough...
👁 185
(PC+WAP) Industrial Machinery and Equipment Website Template – With Filtering and Video Features – 0990

(PC+WAP) Industrial Machinery and Equipment Website Template – With Filtering and Video Features – 0990 Practical Collection pbootcms Template

An industrial machinery and equipment PbootCMS website template compatible with both PC and WAP devices, featuring filtering and video capabilities. The professional industrial design is ideal for machinery and equipment companies to showcase their products, technical specifications, and video case studies. It enables machinery manufacturing enterprises to conduct online multimedia marketing and promotion campaigns. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5...
👁 48