Open fsrm365 opened 8 months ago
@beowulfenator would you please take a look?
I've never used mget
in my projects, so no idea what's going on here. Can you be more specific, @fsrm365?
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}'
@beowulfenator @samdark
Thank you! I'll see what I can do.
https://github.com/yiisoft/yii2-elasticsearch/blob/d697c961f03dc3c9554cfa6e4836db5db00c0310/ActiveRecord.php#L184
Here, the parameter $options is missing. When mget requires a routing index, the missing $options leads to an error.