How to deploy yii2 elasticsearch extension in Yii 1.x

Hi All,

I currently have a Yii 1.x web app and I would like to implement a search feature using popular elasticsearch.

To my knowledge, I know Yii 2.0 has already an established yii-elasticsearch extension and I have tried it using Yii 2.0 basic web app and find that it is useful and I am thinking how can I deploy it into Yii 1.x app?

I managed to make both Yii 1.x and Yii 2.x app running at the same time following the instruction at tutorial here.

The integration looks fine and I created a SearchController and a Search model object at Yii 1.x.

Search Controller




use search\models\Customer;


class DefaultController extends Controller 

{

    /**

     * @return array action filters

     */

    public function filters()

    {

        return array(

            'accessControl', 

        );

    }

    /**

     * Specifies the access control rules.

     * This method is used by the 'accessControl' filter.

     * @return array access control rules

     */

    public function accessRules()

    {

        return array(

            array('allow',  

                'actions'=>array('index'),

                'users'=>array('*'),

            ),

            //default deny all users anything not specified       

            array('deny',  

                'users'=>array('*'),

            ),

        );

    }    

    /**

     * This action shows search index page

     */

    public function actionIndex()

    {

        $matchQuery = ["match" => ["name" => "Test Name"]];

        

        $result = Customer::find()->query($matchQuery)->all(); 

        

        return $this->render('index',['result'=>$result]);

        

    }    

}



Customer search model





namespace search\models;


class Customer extends \yii\elasticsearch\ActiveRecord

{

    /**

     * @return array the list of attributes for this record

     */

    public function attributes()

    {

        // path mapping for '_id' is setup to field 'id'

        return ['id', 'name'];

    }

}



When I try to access the controller via route "search/index" (my main app is Yii 1.x still), I get following error:

PHP Fatal Error – yii\base\ErrorException

Class ‘yii\elasticsearch\ActiveRecord’ not found

I know the reason likely is caused by namespace problem or unable to find yii2 classes.

Any body has idea how to resolve this one?

Many thanks !!

I think there is an ext for 1.1 as well okay here you go

http://www.yiiframework.com/extension/yii-elastica

Hi All,

I managed to get it work by explicitly import yii2-elasticsearch classes.




        $aliases = [

          'yii.elasticsearch.ActiveRecord' => YII2_PATH . '/../yii2-elasticsearch/ActiveRecord',

          'yii.elasticsearch.ActiveQuery' => YII2_PATH . '/../yii2-elasticsearch/ActiveQuery',

          'yii.elasticsearch.Command' => YII2_PATH . '/../yii2-elasticsearch/Command',

          'yii.elasticsearch.Connection' => YII2_PATH . '/../yii2-elasticsearch/Connection',

          'yii.elasticsearch.DebugAction' => YII2_PATH . '/../yii2-elasticsearch/DebugAction',

          'yii.elasticsearch.DebugPanel' => YII2_PATH . '/../yii2-elasticsearch/DebugPanel',

          'yii.elasticsearch.Exception' => YII2_PATH . '/../yii2-elasticsearch/Exception',

          'yii.elasticsearch.Query' => YII2_PATH . '/../yii2-elasticsearch/Query',

          'yii.elasticsearch.QueryBuilder' => YII2_PATH . '/../yii2-elasticsearch/QueryBuilder',

        ];

        foreach ($aliases as $alias => $path) {

            Yii::setPathOfAlias($alias, $path);

            Yii::import($alias);

        }



However, now I have another small problem. The logging in yii2-elasticsearch "Yii:trace(…)" is not appearing anywhere (either Yii1.x or Yii 2.x runtime folder)