-
-
Notifications
You must be signed in to change notification settings - Fork 251
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
Comments
@beowulfenator would you please take a look? |
I've never used |
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:
|
Thank you! I'll see what I can do. |
yii2-elasticsearch/ActiveRecord.php
Line 184 in d697c96
Here, the parameter $options is missing. When mget requires a routing index, the missing $options leads to an error.
The text was updated successfully, but these errors were encountered: