SearchEngine Bundle
A gnugat/search-engine integration in Symfony., (*1)
Caution: this component does not provide actual SearchEngine features, if you're looking for one you should rather have a look at ElasticSearch, Solr, etc.
See gnugat/search-engine
's' website for more information., (*2)
This bundle provides the following services:, (*3)
-
gnugat_search_engine.criteria_factory
: creates Criteria
from Request query parameters
-
gnugat_search_engine.identifier_engine
: an instance of IdentifierEngine
-
gnugat_search_engine.search_engine
: an instance of SearchEngine
-
gnugat_search_engine.type_sanitizer
: an instance of TypeSanitizer
In order for it to work, you need to:, (*4)
- create an implementations of
Fetcher
(or install an existing one, like PommSearchEngine)
- define it as a service with the name
gnugat_search_engine.fetcher
Also, to be able to find anything SearchEngine
and IdentifierEngine
both need you to add information about available resources.
This can be done by implementating SelectBuilder
and define it as a service, for example:, (*5)
services:
app.blog_select_builder:
class: AppBundle\SearchEngine\BlogSelectBuilder
tags:
-
name: gnugat_search_engine.select_builder
resource_name: blog
resource_definition: |
{
"fields": {
"id": "integer",
"title": "string",
"author_id": "integer"
},
"relations": ["author"]
}
We can finally use it, for example in a controller:, (*6)
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class BlogController extends Controller
{
/**
* Full URL example: /v1/blogs?title=IG&sort=author_id,-title&page=2&per_page=1
*
* @Route("/v1/blogs")
* @Method({"GET"})
*/
public function searchAction(Request $request)
{
$criteriaFactory = $this->container->get('gnugat_search_engine.criteria_factory');
$searchEngine = $this->container->get('gnugat_search_engine.search_engine');
$criteria = $criteriaFactory->fromQueryParameters('blog', $request->query->all());
$results = $searchEngine->match($criteria);
return new Response(json_encode($results), 200, array('Content-Type' => 'application/json'));
}
}
Tip: instead of using these services directly in the controller, we can inject
them in other services., (*7)
Installation
To install gnugat/search-engine-bundle
, run the following command:, (*8)
composer require gnugat/search-engine-bundle:^0.3
Then register Gnugat\SearchEngineBundle\GnugatSearchEngineBundle
in AppKernel.php
, (*9)
Further documentation
You can see the current and past versions using one of the following:, (*10)
You can find more documentation at the following links:, (*11)