alamirault / elasticsearch-form-bundle
Post document, search, form listener
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 3
Type:symfony-bundle
Requires
- php: ^7.1
- ruflin/elastica: ^6.1
- symfony/config: ^3.4|^4.0
- symfony/dependency-injection: ^3.4|^4.0
- symfony/form: ^3.4|^4.0
- symfony/http-foundation: ^3.4|^4.0
- symfony/http-kernel: ^3.4|^4.0
- symfony/options-resolver: ^3.4|^4.0
- symfony/security: ^3.4|^4.0
- symfony/serializer: ^3.4|^4.0
- symfony/yaml: ^3.4|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.5
This package is auto-updated.
Last update: 2024-11-30 02:29:20 UTC
README
Overview
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:
- Create a form type, foreach field pass option
elastic_filter_condition
- Use
ElasticsearchMaker
to submit search, paginate, sort.
Installation
-
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(), ... ); ...
Configuration
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'
Usage
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]);
Testing
./vendor/bin/phpunit --bootstrap vendor/autoload.php Tests/