Is there any example of autocomplete with substring highlighting using Yii2 and elasticsearch

Anybody please help. I’ve search all over the internet but didn’t any working example of autocomplete using the elasticsearch in Yii2 with substring highlighting like n-gram.

If you are experiencing problems in the implementation use a simpler librarie and integration support and demos:

https://twitter.github.io/typeahead.js/examples/

Extension yii2

Hi Thanks for your response, but I need to create autocomplete with elasticsearch. Below I’ve shared my code. Currently it show results when I search with full word but no result for substring (like show results for peter but not for pet. Please checkout my code and suggest what I’m doing wrong.

Model Code -


<?php


namespace app\models;


use Yii;


use yii\base\Model;


use yii\elasticsearch\ActiveDataProvider;


use yii\elasticsearch\Query;


use yii\elasticsearch\QueryBuilder;


class Elastic extends \yii\elasticsearch\ActiveRecord

{


    public function attributes()

    {

        return['id', 'name', 'email'];

    }


    public static function mapping()

    {

        return [

            static::type() => [

                'properties' => [

                        'id' => ['type' => 'integer'],

                        'name' => [

                        'type' => 'string',

                        "analyzer" => "autocomplete",

                        "search_analyzer" => "standard",


                    ],

                    'email'    => ['type' => 'string'],

                ]

            ],

        ];

    }


    /**

     * Set (update) mappings for this model

     */

    public static function updateMapping()

    {

        $db = static::getDb();

        $command = $db->createCommand();

        $command->setMapping(static::index(), static::type(), static::mapping());

    }


    /**

     * Create this model's index

     */

    public static function createIndex()

    {

        $db = static::getDb();

        $command = $db->createCommand();

        $command->createIndex(static::index(), [

            'settings' => [

                "number_of_shards" => 1,

                'analysis' => [

                    'analyzer' => [

                        "autocomplete" => [

                            "type" => "custom",

                            "tokenizer" => "standard",

                            "filter" => ["lowercase", "autocomplete_filter"],

                        ],

                    ],

                    'filter' => [

                        "autocomplete_filter" => [

                            "type" => "edge_ngram",

                            "min_gram" => 2,

                            "max_gram" => 15,

                        ],

                    ],


                ],

            ],

            'mappings' => static::mapping(),

            //'warmers' => [ /* ... */ ],

            //'aliases' => [ /* ... */ ],

            //'creation_date' => '...'

        ]);

    }


    /**

     * Delete this model's index

     */

    public static function deleteIndex()

    {

        $db = static::getDb();

        $command = $db->createCommand();

        $command->deleteIndex(static::index(), static::type());

    }


    /**

     * Search this model's index

     */

    public function searchIndex($value)

    {

        var_dump($value);

        $search       = $value['search'];

        $query        = new Query();

        $db           = static::getDb();

        $queryBuilder = new QueryBuilder($db);

        $match        = ['match' => ['name' =>$search]];

        $query->query = $match;

        $build        = $queryBuilder->build($query);

        $re           = $query->search($db, $build);

        $dataProvider = new ActiveDataProvider([

            'query'      => $query,

            'pagination' => ['pageSize' => 10],

        ]);

        return $dataProvider;

   }

}

?>

Here is my Controller Code -


<?php

namespace app\controllers;


use Yii;


use app\models\Elastic;


use yii\web\Controller;


class ElasticController extends Controller

{

    public function actionIndex(){

        return $this->render('index');

    }


    public function actionCreateIndex(){

        $elastic = new Elastic();

        if ($elastic->createIndex()) {

            echo "Index created Successfully";

        } else {

            echo "Error";

        }

    }


    public function actionSearch()

    {

        $elastic = new Elastic();

        $result  = $elastic->searchIndex(Yii::$app->request->queryParams);

        $query = Yii::$app->request->queryParams;

        return $this->render('search', [

            'searchModel'  => $elastic,

            'dataProvider' => $result,

            'query'        => $query['search'],

        ]);

    }


    public function actionCreate()

    {

        $elastic = new Elastic();

        $elastic->name  = 'Peter';

        $elastic->email = 'peterparker@gmail.com';

        if ($elastic->insert()) {

            echo "Added Successfully";

        } else {

            echo "Error";

        }

    }

}

?>