-
Notifications
You must be signed in to change notification settings - Fork 793
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
Multi type search to multi index search #1385
Comments
Very new to ES and this bundle, but I've got as far as being able to perform a search across multiple indices;
In this case |
Your solution work fine. Thank you very much |
full article here: https://www.exploit.cz/how-to-search-across-multiple-elasticsearch-indexes-with-symfony-fos-elasticabundle/ define service FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection:
arguments:
- {
recipe: '@fos_elastica.elastica_to_model_transformer.recipe.recipe',
recipes: '@fos_elastica.elastica_to_model_transformer.recipes.recipes'
} define custom class and inject this collection into $this->elasticaToModelTransformerCollection; $index = $this->indexManager->getIndex('recipe');
$index2 = $this->indexManager->getIndex('recipes');
$searchable = new MultiIndex($index->getClient(), $index->getName());
$searchable->addIndex($index);
$searchable->addIndex($index2);
$transformer = $this->elasticaToModelTransformerCollection;
$paginatorAdapter = new TransformedPaginatorAdapter($searchable, $query, [], $transformer);
$items = new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
$items->setMaxPerPage($limit);
$items->setCurrentPage($page); where MultiIndex looks like <?php
declare(strict_types=1);
/*
* Created by Exploit.cz <insekticid@exploit.cz>
*/
namespace App\Search\Elastica;
use Elastica\Exception\InvalidException;
use Elastica\Index;
use Elastica\ResultSet\BuilderInterface;
use Elastica\Search;
class MultiIndex extends Index
{
/**
* Array of indices.
*
* @var array
*/
protected $_indices = [];
/**
* Adds a index to the list.
*
* @param \Elastica\Index|string $index Index object or string
*
* @throws \Elastica\Exception\InvalidException
*
* @return $this
*/
public function addIndex($index)
{
if ($index instanceof Index) {
$index = $index->getName();
}
if (!is_scalar($index)) {
throw new InvalidException('Invalid param type');
}
$this->_indices[] = (string) $index;
return $this;
}
/**
* Add array of indices at once.
*
* @param array $indices
*
* @return $this
*/
public function addIndices(array $indices = [])
{
foreach ($indices as $index) {
$this->addIndex($index);
}
return $this;
}
/**
* Return array of indices.
*
* @return array List of index names
*/
public function getIndices()
{
return $this->_indices;
}
/**
* @param string|array|\Elastica\Query $query
* @param int|array $options
* @param BuilderInterface $builder
*
* @return Search
*/
public function createSearch($query = '', $options = null, BuilderInterface $builder = null)
{
$search = new Search($this->getClient(), $builder);
$search->addIndices($this->getIndices());
$search->setOptionsAndQuery($options, $query);
return $search;
}
} |
The docs are still wrong for elasticsearch 6.0 and all following versions. Since this issue was closed: there is another open issue: #1521 |
Yeah I just ran into this and there are no great alternatives. It would be nice if this bundle got updated to handle multiple indexes in a better way. I am building a global site search atm and indexes are only added if the user has permissions. Getting that to work along with pagination is pretty god awful right now. |
I used this response but I had problem with searching on other index than the one I used to get de transformer (thread in this example). To avoid that, I override the class "ElasticaToModelTransformerCollection" like this :
In the OverrideServiceCompilerPass.php :
Then in my service using the transformer, I create a new transformer : Service arguments :
And in my service :
It's seems to work fine for me but I'm not against a better solution. :) |
Hello,
|
My Article updated, works well with latest master 6.0 Beta (need to create new ElasticaToModelTransformerCollection class) |
@insekticid your post works pretty well but I updated a few things: When defining the ElasticaToModelTransformerCollection you pass each transformer as an argument in an array. This is unnecessary because all transformers are tagged with
And the
And now all indexes get auto added automatically. I found it annoying that every time I added a new index I had to update my Besides that good job and thanks. You saved me some time. |
Currently having the same issue as well. Saw that the class was added to master, but was later removed in commit 926919195c2efaca2fb0b42304ca749780645295. |
Hello there,
The new elasticsearch don't allow to have more than one type by index.
So this doc is Obsolete, isn't it ?
https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/doc/cookbook/multi-type-search.md
I'am looking for do similar thing, but now with multi index search. How should I do ?
Thanks
The text was updated successfully, but these errors were encountered: