After a long hiatius I thought it would be a good time to write a new blog post. I’m currently in the process of rewriting the FMDB from scratch using the Symfony 2 framework which is awesome.
Inspired by the recents posts from Richard Miller regarding Symfony 2 & Elasticsearch, this post deals with the question how you can search multiple types at once using elasticsearch.
Assuming you already have multiple types indexed by Elasticsearch and want to build some kind of overall search you need to add the wanted types to the search process. The following code samples are courtesy of Richard Miller who granted my permission to use them in my post.
To now search for more then one type at the time we first need to get the client from the DIC:
/** * @Route("/sites/search/", name="site_search") * @Method({ "head", "get" }) * @Template */ public function searchAction(Request $request) { $client = $this->get('foq_elastica.client'); $search = new \Elastica_Search($client); $postType = $this->get('foq_elastica.index.acme.post'); $tagType = $this->get('foq_elastica.index.acme.tag'); // add the types to the search $search->addType($postType) ->addType($tagType); $index = $this->get('foq_elastica.index'); $search->addIndex($index); $searchTerm = $request->query->get('terms'); $postSubjectQuery = new \Elastica_Query_Text(); $postSubjectQuery->setFieldQuery('subject', $searchTerm); $postSubjectQuery->setFieldParam('subject', 'analyzer', 'snowball'); $tagQuery = new \Elastica_Query_Text(); $tagQuery->setFieldQuery('tagname', $searchTerm); $tagQuery->setFieldParam('tagname', 'analyzer', 'snowball'); $boolQuery = new \Elastica_Query_Bool(); $boolQuery->addShould($nameQuery); $boolQuery->addShould($keywordsQuery); $results = $search->search($boolQuery); return array('results' => $results); }
That’s it basically. One thing that is obviously missing is the further processing of $results to split it into the distinct types and fetch the entities represented by them.
I want to paginate this result. Is it posible?
Hi Yosmany,
yes, this is possible and you can now use FOQElasticaBundle to search multiple types. See the “Index wide finder”-section of http://bit.ly/ADELsJ
.
To use pagination there is an example in the “Doctrine/Propel finder”-section.
I hope that helps.
- Marcus
Well, I know how to paginate a result. And how to search multiple types at once. But can’t see how to paginate a result from multiple types at once.
I can’t see how to pass a search to TransformedFinder constructor, because it require an object implementing Elastica_Searchable.