### Consume Queue Messages (Setup Broker) Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/doctrine-queue-listener.md Run the enqueue:consume command with the --setup-broker flag to start processing messages from the queue. This is the recommended approach. ```bash $ ./bin/console enqueue:consume --setup-broker -vvv ``` -------------------------------- ### Install EnqueueElasticaBundle and Filesystem Transport Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/doctrine-queue-listener.md Install the necessary composer packages for EnqueueElasticaBundle and a message queue transport like the filesystem transport. ```bash $ composer require enqueue/elastica-bundle:^0.8.1 enqueue/fs:^0.8 ``` -------------------------------- ### Install enqueue/elastica-bundle and enqueue/sqs Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/speed-up-populate-command-sqs.md Install the necessary Composer packages for integrating FOS Elastica with AWS SQS via the enqueue bundle. ```bash composer require enqueue/elastica-bundle composer require enqueue/sqs ``` -------------------------------- ### Implement PagerProviderInterface Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/manual-provider.md Create a PHP class that implements the PagerProviderInterface and provides the 'provide' method to return a PagerInterface instance. This example uses Pagerfanta with an ArrayAdapter. ```php find($query); } } ``` -------------------------------- ### Run Enqueue Consumers Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/speed-up-populate-command.md Start the message queue consumers. Running multiple consumers can improve performance. Use the first command for general use and the second if the first is not suitable. ```bash $ ./bin/console enqueue:consume --setup-broker -vvv ``` ```bash $ ./bin/console enqueue:transport:consume enqueue_elastica.populate_processor -vvv ``` -------------------------------- ### Basic FOSElasticaBundle Configuration Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/setup.md Configure a minimal setup with one Elasticsearch client and one index named 'app'. The index 'app' will be available as the service `fos_elastica.index.app`. ```yaml #app/config/config.yml fos_elastica: clients: default: { hosts: ['http://localhost:9200'] } indexes: app: ~ ``` -------------------------------- ### Using Index-Wide Finder Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/usage.md Get and use the index-wide finder service 'fos_elastica.finder.app' to search across all mapped objects in the index. ```php /** var FOS\ElasticaBundle\Finder\MappedFinder */ $finder = $this->container->get('fos_elastica.finder.app'); // Returns a mixed array of any objects mapped $results = $finder->find('bob'); ``` -------------------------------- ### Require FOSElasticaBundle with Composer Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/setup.md Use Composer to download the latest stable version of the FOSElasticaBundle. This requires Composer to be installed globally. ```bash $ composer require friendsofsymfony/elastica-bundle ``` -------------------------------- ### Using Aggregations with Paginated Finder Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/usage.md Retrieve aggregations when using paginated methods on the finder. This example adds a 'Terms' aggregation on the 'companyGroup' field. ```php $query = new \Elastica\Query(); $agg = new \Elastica\Aggregation\Terms('tags'); $agg->setField('companyGroup'); $query->addAggregation($agg); $companies = $this->finder->findPaginated($query); $companies->setMaxPerPage($params['limit']); $companies->setCurrentPage($params['page']); $aggs = $companies->getAdapter()->getAggregations(); ``` -------------------------------- ### Custom Index Settings Configuration Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/indexes.md Specify custom index settings, such as enabling a custom analyzer. This example defines a custom analyzer 'my_analyzer' with a 'lowercase' tokenizer and an 'nGram' filter. ```yaml fos_elastica: indexes: blog: settings: index: analysis: analyzer: my_analyzer: type: custom tokenizer: lowercase filter : [my_ngram] filter: my_ngram: type: "nGram" min_gram: 3 ``` -------------------------------- ### Setting Client Options for Elastica HTTP Client Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/elastica-http-client-configuration.md Pass raw options directly to the underlying HTTP client (Guzzle, Symfony HTTP Client, or bundled Curl client) using the `client_options` key. This example shows options for Guzzle/Symfony and the bundled Curl client. ```yaml # app/config/config.yml fos_elastica: clients: default: hosts: ['http://example.com:80'] client_options: # Guzzle / Symfony HTTP Client timeout: 30 connect_timeout: 10 proxy: 'http://localhost:8125' # elastic-transport's bundled Curl client (used when neither Guzzle nor # Symfony HTTP Client is installed). Use CURLOPT_* integer keys: !php/const \CURLOPT_TIMEOUT: 30 !php/const \CURLOPT_CONNECTTIMEOUT: 10 !php/const \CURLOPT_RANDOM_FILE: /dev/urandom ``` -------------------------------- ### Setting HTTP Headers for Elastica Client Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/elastica-http-client-configuration.md Configure custom HTTP headers, such as Authorization, for the Elastica client. This example uses the deprecated top-level `headers` key. ```yaml # app/config/config.yml fos_elastica: clients: default: hosts: ['http://example.com:80'] headers: Authorization: "Basic jdumrGK7rY9TMuQOPng7GZycmxyMHNoir==" ``` -------------------------------- ### Date Format Configuration Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/indexes.md Specify custom date formats for date type fields. This example shows formats for 'lastlogin' and 'birthday' fields. ```yaml fos_elastica: indexes: user: properties: username: { type: text } lastlogin: { type: date, format: basic_date_time } birthday: { type: date, format: "yyyy-MM-dd" } ``` -------------------------------- ### Example SQS message payload Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/speed-up-populate-command-sqs.md This is an example of the JSON payload that will be added to the AWS SQS queue for processing. ```json { "options": { "max_per_page": 1000, "delete": true, "reset": true, "ignore_errors": false, "sleep": 0, "indexName": "user" }, "page": 1 } ``` -------------------------------- ### Implement PreTransformEvent Listener Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/pre-transform-event.md Implement an event subscriber to listen for PreTransformEvent and perform custom operations on the object before it's transformed. This example reloads the object's translation. ```php namespace AcmeBundle\EventListener; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use FOS\ElasticaBundle\Event\PreTransformEvent; class PreTransformListener implements EventSubscriberInterface { private $anotherService; // ... public function doPreTransform(PreTransformEvent $event) { $this->anotherService->reloadTranslation($event->getObject()); } public static function getSubscribedEvents() { return [ PreTransformEvent::class => 'doPreTransform', ]; } } ``` -------------------------------- ### Nested Objects Configuration Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/indexes.md Configure nested objects and object types within index properties. This example demonstrates mapping for 'comments' as a nested type and 'user'/'approver' as object types with their own properties. ```yaml fos_elastica: indexes: post: properties: date: { boost: 5 } title: { boost: 3 } content: ~ comments: type: "nested" properties: date: { boost: 5 } content: ~ user: type: "object" approver: type: "object" properties: date: { boost: 5 } ``` -------------------------------- ### Dynamic Index Naming based on Environment Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/setup.md Configure the index name to dynamically include the kernel environment, for example, 'app_dev' for the development environment. This allows for environment-specific Elasticsearch indexes. ```yaml #app/config/config.yml fos_elastica: indexes: app: index_name: app_%kernel.environment% ``` -------------------------------- ### Autowire Index and Finder in Controller Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/usage.md Demonstrates how to autowire FOSElasticaBundle Index and TransformedFinder services into a Symfony controller for easy access to search functionality. ```php namespace App\Controller; use FOS\ElasticaBundle\Elastica\Index; use FOS\ElasticaBundle\Finder\TransformedFinder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/default', name: 'app_default')] public function index( Index $blogPostIndex, TransformedFinder $blogPostFinder ): Response { $results = $blogPostFinder->findPaginated('search text'); $resultsPage = $results->getCurrentPageResults(); } ``` -------------------------------- ### Populate Elasticsearch Indexes Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/aliased-indexes.md Run the `fos:elastica:populate` command to populate the Elasticsearch indexes. Ensure caches are cleared before execution. ```bash bin/console -eprod 'fos:elastica:populate' ``` -------------------------------- ### Enable FOSElasticaBundle in config/bundles.php Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/setup.md Add the FOSElasticaBundle to your project's `config/bundles.php` file to enable it. ```php ['all' => true], ... ]; ``` -------------------------------- ### Consume Queue Messages (Directly) Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/doctrine-queue-listener.md Alternatively, use the enqueue:transport:consume command to process messages directly. Use this only if the --setup-broker option is not feasible. ```bash $ ./bin/console enqueue:transport:consume enqueue_elastica.doctrine.sync_index_with_object_change_processor -vvv ``` -------------------------------- ### Run Populate Command with Queue Persister Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/speed-up-populate-command.md Execute the populate command with the `--pager-persister=queue` option to utilize the message queue for processing. ```bash $ ./bin/console fos:elastica:populate --pager-persister=queue ``` -------------------------------- ### Configure Index-Wide Finder Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/usage.md Enable an index-wide finder by setting 'finder: ~' in the fos_elastica configuration for the 'app' index. ```yaml fos_elastica: indexes: app: finder: ~ ``` -------------------------------- ### Implement HighlightableModelInterface for Entities Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/attachments.md Implement the HighlightableModelInterface to handle search highlights. This requires getId(), setElasticHighlights(), and getElasticHighlights() methods. ```php use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; class Library implements HighlightableModelInterface { private $id private $highlights; //Needs this method for HighlightableModelInterface public function getId() { return $this->id; } //Needs this method for HighlightableModelInterface public function setElasticHighlights(array $highlights) { $this->highlights = $highlights; return $this; } public function getElasticHighlights() { return $this->highlights; } } ``` -------------------------------- ### Configure Finder Service Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/usage.md Configure a finder service for the 'user' index in services.yaml. This allows for hydrated search results. ```yaml # config/services.yaml services: # ... App\Controller\UserController: tags: ['controller.service_arguments'] public: true arguments: - '@fos_elastica.finder.user' ``` -------------------------------- ### Check Indexes During Populate Process Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/cookbook/aliased-indexes.md Monitor the Elasticsearch aliases during the `fos:elastica:populate` command to observe the creation of new index versions and alias assignments. ```bash in other cli in // check indexes during populate process curl -XGET 'http://localhost:9200/_alias/?pretty' { "app" : { "aliases" : { "app_prod" : { } } }, "app_prod_2019-05-28-153852" : { "aliases" : { } } } ``` -------------------------------- ### Configure Index Templates Source: https://github.com/friendsofsymfony/foselasticabundle/blob/master/doc/templates.md Define index templates in your configuration file to automatically apply settings and mappings to new indices. Specify index patterns, settings, and type mappings. ```yaml # app/config/config.yml fos_elastica: index_templates: : client: default template_name: