Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a bug here #348

Open
fsrm365 opened this issue Mar 5, 2024 · 5 comments
Open

There is a bug here #348

fsrm365 opened this issue Mar 5, 2024 · 5 comments
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@fsrm365
Copy link

fsrm365 commented Mar 5, 2024

$model = static::get(reset($primaryKeys));

Here, the parameter $options is missing. When mget requires a routing index, the missing $options leads to an error.

@samdark samdark added type:bug Bug status:to be verified Needs to be reproduced and validated. labels Mar 5, 2024
@samdark
Copy link
Member

samdark commented Mar 5, 2024

@beowulfenator would you please take a look?

@beowulfenator
Copy link
Collaborator

I've never used mget in my projects, so no idea what's going on here. Can you be more specific, @fsrm365?

@fsrm365
Copy link
Author

fsrm365 commented Mar 7, 2024

I've never used mget in my projects, so no idea what's going on here. Can you be more specific, @fsrm365?

Firstly, create an AR class: Students.php

<?php

namespace app\models\elasticsearch;

use yii\base\InvalidConfigException;
use yii\elasticsearch\ActiveRecord;
use yii\elasticsearch\Exception;

class Students extends ActiveRecord
{
    /**
     * @return array This model's mapping
     */
    public static function mapping()
    : array
    {
        return [
            'dynamic'    => 'strict',
            '_routing'   => ['required' => true],
            "properties" => [
                "number" => ["type" => "keyword"],
                "name"   => ["type" => "keyword"],
            ],
        ];
    }

    /**
     * @return array|string[]
     */
    public function attributes()
    : array
    {
        $mapping = static::mapping();
        return array_keys($mapping['properties']);
    }

    /**
     * @return mixed
     * @throws Exception
     * @throws InvalidConfigException
     */
    public static function createIndex()
    {
        $db      = static::getDb();
        $command = $db->createCommand();
        $config  = [
            //'aliases' => [ /* ... */ ],
            'mappings' => [static::type() => static::mapping()],
        ];
        return $command->createIndex(static::Index(), $config);
    }

    public static function index()
    : string
    {
        return 'students';
    }


}

Next, create a command that is responsible for creating an Elasticsearch index, adding data to the index, and retrieving data from it.

<?php

namespace app\commands;

use app\models\elasticsearch\Students;
use yii\console\Controller;

class StudentsController extends Controller
{
    public function actionCreateIndex()
    {
        Students::createIndex();
        echo 'Elasticsearch index has been created.', PHP_EOL;
    }

    public function actionAddStudents()
    {
        $student1         = new Students();
        $student1->number = '1';
        $student1->name   = 'test name 1';
        $student1->set_id('1');
        $student1->insert(true, null, ['routing' => 'class1']);
        $student2         = new Students();
        $student2->number = '2';
        $student2->name   = 'test name 2';
        $student2->set_id('2');
        $student2->insert(true, null, ['routing' => 'class1']);
    }

    public function actionMgetOne()
    {
        $students = Students::mget(['1'], ['routing' => 'class1']);
        echo "Number of students obtained: ", count($students), PHP_EOL;
    }

    public function actionMgetTwo()
    {
        $students = Students::mget(['1', '2'], ['routing' => 'class1']);
        echo "Number of students obtained: ", count($students), PHP_EOL;
    }
}

Run this commands:

./yii students/create-index
./yii students/add-students
./yii students/mget-one
./yii students/mget-two

The command: ./yii students/mget-one is reporting an error:

Exception 'yii\elasticsearch\Exception' with message 'Elasticsearch request failed with code 400. Response body: {"error":{"root_cause":[{"type":"routing_missing_exception","reason":"routing is required for [students]/[students]/[1]","index_uuid":"_na_","index":"students"}],"type":"routing_missing_exception","reason":"routing is required for [students]/[students]/[1]","index_uuid":"_na_","index":"students"},"status":400}'

@fsrm365
Copy link
Author

fsrm365 commented Mar 7, 2024

@beowulfenator @samdark

@beowulfenator
Copy link
Collaborator

Thank you! I'll see what I can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
None yet
Development

No branches or pull requests

3 participants