alamirault/elasticsearch-form-bundle
This Symfony bundle allows to create Form and transform fields to an elasticsearch query. Once you created your form type you will be able to update an elasticsearch query from a form type.
The idea is:
elastic_filter_conditionElasticsearchMaker to submit search, paginate, sort.Run composer require alamirault/elasticsearch-form-bundle
Register the bundle in your app/AppKernel.php:
<?php
...
public function registerBundles()
{
$bundles = array(
...
new Alamirault\ElasticsearchBundle\AlamiraultElasticsearchFormBundle(),
...
);
...
alamirault_elasticsearch_form:
clients:
default:
connections:
- { host: "elasticsearch_host", port: 9200, proxy: null }
logger: logger.api
indexes:
users:
mapping: '%kernel.project_dir%/src/Alamirault/LogsBundle/Resources/elasticsearch/mapping/change.yml'
In ExampleFilterType class:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('uuid', TextType::class, [
'elastic_filter_condition' => function (?string $value) {
if (is_null($value)) {
return;
}
return new Match('uuid', $value);
},
])
}
In controller to make a search page list:
public function indexAction(Request $request, FormFactoryInterface $formFactory, EntityManagerInterface $entityManager,
TranslatorInterface $translator, BoLogEntryDenormalizer $denormalizer)
{
$form = $formFactory->create(ExampleFilterType::class);
$sortableChanges = $this->elasticsearchMaker->manageForm($form, $request, $this->indexRegistry,
"changes", $denormalizer);
return $this->render('AlamiraultLogsBundle:Changes:index.html.twig',
[
"form" => $form->createView(),
"sortableChanges" => $sortableChanges,
]
);
}
Search users by uuid begins by value:
$matchLike = new Query\Wildcard('uuid', $value."*");
$query = new Query();
$query->setQuery($matchLike);
$query->setSource(["object_uuid"]);
$resultSet = $this->elasticsearchMaker->makeSearch($this->indexRegistry, "users", $query);
Publish document
$normalized = $serializer->normalize($user) //User is an object;
$doc = new Document($user->getUuid(), $normalized);
$index = $this->indexMaker->getIndex($this->indexRegistry, "users", IndexMaker::WRITE);
$type = new Type($index, '_doc'); // Only if elasticsearch <7.2.0
$type->addDocuments([$doc]);
./vendor/bin/phpunit --bootstrap vendor/autoload.php Tests/
How can I help you explore Laravel packages today?