I have 2 indices documents_1
and documents_2
in ElasticSearch with different mapping types (they have some different attributes). I am using Yii2
framework on php
to search on web.
Question: How can I search over 2 indices (if it is possible) and merge results in one sorted by date for example? To explain I do need something like this:
$queryOne = DocumentOne::find()->all();
$queryTwo = DocumentTwo::find()->all();
merge_results($queryOne, $queryTwo);
DocumentOne.php
class DocumentOne extends \yii\elasticsearch\ActiveRecord
{
public static function index()
{
return 'documents_1';
}
public static function type()
{
return 'type_a';
}
public function attributes()
{
return ['id', 'any_1', 'created_format_1'];
}
}
DocumentTwo.php
class DocumentTwo extends \yii\elasticsearch\ActiveRecord
{
public static function index()
{
return 'documents_2';
}
public static function type()
{
return 'type_b';
}
public function attributes()
{
return ['id', 'any_2', 'created_format_2'];
}
}
DocumentOneSearch.php
class DocumentOneSearch extends DocumentOne
{
public function search($params)
{
$query = DocumentOne::find();
$sort = new Sort([
'defaultOrder' => [
'created_format_1' => SORT_DESC,
],
]);
return $query
->orderBy($sort->orders)
->all();
}
}