wordpress中文搜索,分词搜索,智能中文搜索检索开源插件ChineseSearchPro代码分析

wordpress自带的中文搜索体验很不好。常常搜不到已有的文章。

比如站内某篇文章标题是:

wordpress开发人员必备的20个工具

如果你的客户搜索“wordpress必备工具”就找不到这篇文章

因为wordpress自带的搜索器只能搜“wordpress开发人员”或“必备的20个工具”或“wordpress开发人员必备的20个工具”这种对照标题连在一起的词语才能搜到这篇文章。像“wordpress开发工具”或“wordpress开发必备工具”这种不是连词就搜不到。ChineseSearchPro插件就完美解决这种问题,自动解析中文语句的词组,让wordpress中文搜索更智能,能轻松搜索到更多相关文章。

ChineseSearchPro插件无需任何第三方付费api接口,底层实现中文词组智能分析解析,优化你的中文搜索。

搜索关键词

wordpress

ChineseSearchPro

√(搜到)×(搜不到)

√(搜到)×(搜不到)

wordpress开发人员必备

开发人员必备

必备的20个工具

wordpress必备工具

×

wordpress开发工具

×

开发必备工具

×

开发工具wordpress

×

ChineseSearchPro插件是免费开源的插件,随意使用。已测试兼容最新wordpress版本和目前各种主题。

插件下载地址:wordpress智能中文搜索系统ChineseSearchPro插件-C4D学习社(原设计1加1)

底层智能中文搜词代码_

<?php
/**
 * 1.Elasticsearch检索引擎模型
 */
namespace app\index\model;
use Elasticsearch\ClientBuilder;  
   
class Elasticsearch
{
    //配置
    private $config = [
        'hosts' => ['http://127.0.0.1:9200']
    ];
    private $api;
    public function __construct()
    {
        #include(APP_PATH .'/vendor/autoload.php');
        #require_once EXTEND_PATH . 'org/elasticsearch/autoload.php';
        import('org.elasticsearch.autoload', EXTEND_PATH);
        $this->api = ClientBuilder::create()->setHosts($this->config['hosts'])->build(); 
    }
 
    /*************************************************************
    /**
     * 索引一个文档
     * 说明:索引没有被创建时会自动创建索引
     */
    public function addOne()
    {
        $params = [];  
        $params['index'] = 'xiaochuan';  
        $params['type']  = 'cat';  
        $params['id']  = '20180407001';  # 不指定就是es自动分配
        $params['body']  = array('name' => '小川编程');  
        return $this->api->index($params);
    }
 
    /**
     * 索引多个文档
     * 说明:索引没有被创建时会自动创建索引
     */
    public function addAll()
    {
        $params = [];
        for($i = 1; $i < 21; $i++) {  
            $params['body'][] = [
                'index' => [
                    '_index' => 'test_index'.$i,
                    '_type'  => 'cat_test',
                    '_id'    => $i,
                ]
            ];  
            $params['body'][] = [  
                'name' => '小川编程'.$i,  
                'content' => '内容'.$i  
            ];
        }  
        return $this->api->bulk($params);  
    }
 
    /**
     * 获取一个文档
     */
    public function getOne()
    {
        $params = [];  
        $params['index'] = 'xiaochuan';  
        $params['type']  = 'cat';  
        $params['id']    = '20180407001';  
        return $this->api->get($params); 
    }
 
    /**
     * 搜索文档
     */
    public function search()
    {
        $params = [];
        $params['index'] = 'xiaochuan';  
        $params['type']  = 'cat';  
        $params['body']['query']['match']['name'] = '小川编程';  
        return $this->api->search($params); 
    }
 
    /**
     * 删除文档
     * 说明:文档删除后,不会删除对应索引。
     */
    public function delete()
    {
        $params = [];  
        $params['index'] = 'xiaochuan';  
        $params['type'] = 'cat';  
        $params['id'] = '20180407001';  
        return $this->api->delete($params);  
    }
 
    /*************************************************************
    /**
     * 创建索引
     */
    public function createIndex()
    {
        $params = [];
        $params['index']  = 'xiaochuan'; 
        return $this->api->indices()->create($params);  
    }
       
      /**
     * 删除索引:匹配单个 | 匹配多个
     * 说明: 索引删除后,索引下的所有文档也会被删除
     */
      public function deleteIndex()
      {  
          $params = [];
          $params['index'] = 'test_index';  # 删除test_index单个索引
          #$params['index'] = 'test_index*'; # 删除以test_index开始的所有索引
        return $this->api->indices()->delete($params);  
      }
 
      /*************************************************************
      /**
     * 设置索引配置
     */
      public function setIndexConfig()
      {  
          $params = [];
          $params['index'] = 'xiaochuan';  
        $params['body']['index']['number_of_replicas'] = 0;  
        $params['body']['index']['refresh_interval'] = -1;  
        return $this->api->indices()->putSettings($params);  
      }
 
      /**
     * 获取索引配置
     */
      public function getIndexConfig()
      {
          # 单个获取条件写法
        $params['index'] = 'xiaochuan';  
        # 多个获取条件写法
        //$params['index'] = ['xiaochuan', 'test_index'];  
        return $this->api->indices()->getSettings($params);  
      }
 
    /**
     * 设置索引映射
     */
      public function setIndexMapping()
      {
          #  设置索引和类型 
        $params['index'] = 'xiaochuan';  
        $params['type']  = 'cat';  
           
        #  向现有索引添加新类型
        $myTypeMapping = array(  
            '_source' => array(  
                'enabled' => true  
            ),  
            'properties' => array(  
                'first_name' => array(  
                    'type' => 'string',  
                    'analyzer' => 'standard'  
                ),  
                'age' => array(  
                    'type' => 'integer'  
                )  
            )  
        );  
        $params['body']['cat'] = $myTypeMapping;  
           
        #  更新索引映射 
        $this->api->indices()->putMapping($params);  
      }
 
      /**
     * 获取索引映射
     */
      public function getIndexMapping()
      {  
          #  获取所有索引和类型的映射  
        $ret = $this->api->indices()->getMapping();  
         
        /*  
        #  获取索引为:xiaochuan的映射
        $params['index'] = 'xiaochuan';  
        $ret = $this->api->indices()->getMapping($params);  
           
        #  获取类型为:cat的映射
        $params['type'] = 'cat';  
        $ret = $this->api->indices()->getMapping($params);  
           
        #  获取(索引为:xiaochuan和 类型为:cat)的映射
        $params['index'] = 'xiaochuan';  
        $params['type']  = 'cat'  
        $ret = $this->api->indices()->getMapping($params);  
           
        #  获取索引为:xiaochuan和test_index的映射
        $params['index'] = ['xiaochuan', 'test_index'];  
        $ret = $this->api->indices()->getMapping($params); 
        */
 
        return $ret;
      }
 
 
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值