Yii 1.1.*集成elasticsearch php 客户端Elastica

Yii  是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。Yii提供了今日Web 2.0应用开发所须要的几乎一切功能。Yii是最有效率的PHP框架之一。官方网站  php

ElasticSearch  是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,可以达到实时搜索,稳定,可靠,快速,安装使用方便。支持经过HTTP使用JSON进行数据索引。官方网站 git

Elastica  是用php写的elasticsearch客户端,经过Elastica可很方便的在php应用中访问elasticsearch,如:建立索引,添加文档等。官方网站
github

在Yii中集成elasticsearch其实很是简单: app

第一步:将下载的Elastica整个拷贝到protected/vendors/目录下(注意:若是是从github上clone下来的,应该是Elastica下lib下面的Elastica 框架

第二步:编写Elastica类自动导入文件,如 ElasticaAutoLoader.php yii

<?php

/**
 * Description of ElasticaAutoLoader
 *
 * @author Owner
 */
class ElasticaAutoLoader {

    /**
     * @var array class prefixes
     */
    static $prefixes = array(
        'Elastica'
    );

    /**
     * @var string path to where Zend classes root is located
     */
    static $basePath = null;

    /**
     * Class autoload loader.
     *
     * @static
     * @param string $className
     * @return boolean
     */
    static function loadClass($className) {
        foreach (self::$prefixes as $prefix) {
            if (strpos($className, $prefix . '_') !== false) {
                if (!self::$basePath)
                    self::$basePath =
                            Yii::getPathOfAlias("application.vendors") . '/';
                include self::$basePath . str_replace('_', '/', $className) . '.php';
                return class_exists($className, false) ||
                        interface_exists($className, false);
            }
        }
        return false;
    }

}

?>

第三步:修改index.php文件 elasticsearch

分布式

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);  require_once($yii);
Yii::createWebApplication($config)->run();
改成:

<?php

// change the following paths if necessary
$yii = dirname(__FILE__) . '/../yii/framework/yii.php';
$config = dirname(__FILE__) . '/protected/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
require_once(dirname(__FILE__) . '/protected/functions/yii.php');
require_once(dirname(__FILE__) . '/protected/functions/functions.php');
require_once($yii);
$app = Yii::createWebApplication($config);
 
// adding custom Zend Framework autoloader
Yii::import("application.vendors.*");
Yii::import("application.components.ElasticaAutoLoader", true);
Yii::registerAutoloader(array('ElasticaAutoLoader','loadClass'), true);
 
$app->run();
完成上面几步就能够直接在程序中使用了!
相关文章
相关标签/搜索