PHP code example of doopcompany / doop-elasticsearch-bundle
1. Go to this page and download the library: Download doopcompany/doop-elasticsearch-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
doopcompany / doop-elasticsearch-bundle example snippets
return [
// ...
Doop\ElasticsearchBundle\DoopElasticsearchBundle::class => ['all' => true],
];
namespace App\EventListener;
use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Massive\Bundle\SearchBundle\Search\Event\PreDeindexEvent;
use Massive\Bundle\SearchBundle\Search\Event\PreIndexEvent;
use Massive\Bundle\SearchBundle\Search\SearchEvents;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\SearchBundle\Search\Document;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyArticleMassiveSearchSubscriber implements EventSubscriberInterface
{
private DocumentManagerInterface $documentManager;
private Client $client;
public function __construct(
DocumentManagerInterface $documentManager,
Client $client
) {
$this->documentManager = $documentManager;
$this->client = $client;
}
public static function getSubscribedEvents(): array
{
return [
SearchEvents::PRE_INDEX => 'onPreIndex',
SearchEvents::PRE_DEINDEX => 'onPreDeIndex',
];
}
public function onPreDeIndex(PreDeindexEvent $event)
{
/** @var Document $document */
$document = $event->getDocument();
if (ArticleDocument::class !== $document->getClass() || 'my_article' !== $document->getField('_structure_type')->getValue()) {
return;
}
try {
$this->client->delete([
'index' => 'my_index',
'id' => $document->getId(),
]);
} catch (Missing404Exception) {
}
}
/**
* @throws DocumentManagerException
*/
public function onPreIndex(PreIndexEvent $event)
{
/** @var Document $document */
$document = $event->getDocument();
if (ArticleDocument::class !== $document->getClass() || 'my_article' !== $document->getField('_structure_type')->getValue()) {
return;
}
/** @var ArticleDocument $article */
$article = $this->documentManager->find($document->getId());
$structure = $article->getStructure()->toArray();
$extensions = $article->getExtensionsData()->toArray();
$this->client->index([
'index' => 'my_index',
'id' => $document->getId(),
'body' => [
'id' => $document->getId(),
'title' => $structure['title'],
'date' => $structure['date'] ?? null,
'geo_point' => $structure['geo_point'] ?? null,
'route_path' => $structure['routePath'],
'description' => $extensions['excerpt']['description'],
'excerpt' => [
'categories' => $this->formatCategories($extensions['excerpt']['categories']),
],
],
]);
}
private function formatCategories(array $categoryIds): array
{
$formatted = [];
foreach ($categoryIds as $categoryId) {
$formatted[] = ['id' => $categoryId];
}
return $formatted;
}
}