Yii是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。Yii是最有效率的PHP框架之一。
ElasticSearch 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。支持通过HTTP使用JSON进行数据索引。
Elastica 是用php写的elasticsearch客户端,通过Elastica可很方便的在php应用中访问elasticsearch,如:创建索引,添加文档等。
在Yii中集成elasticsearch其实非常简单:
第一步:将下载的Elastica整个拷贝到protected/vendors/目录下(注意:如果是从github上clone下来的,应该是Elastica下lib下面的Elastica)
第二步:编写Elastica类自动导入文件,如ElasticaAutoLoader.php
<?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文件
将
<?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();
完成上面几步就可以直接在程序中使用了!