Extending
=========
You can extend the bundle by customizing the Factory class and with custom metadata drivers.
Factory
-------
The factory service can be customized, enabling you to instantiate your own
classes for use in any listeners which you register. For example, you want to
add a "thumbnail" field to the Document object and create a custom document
``MyCustomDocument``:
.. code-block:: php
You can also implement standard JMS serializer drivers. This would be optimal
if you only need the class name to determine the metadata. Extend the
``Metadata\Driver\DriverInterface`` and add the tag
``massive_search.metadata.driver`` tag to your implementations service
definition.
.. code-block:: xml
.. note::
Adding new metadata providers is non-trivial, you should check the
existing code for implementation details.
Converters
----------
Massive Search allows you to implement custom types. This types can be defined
with custom converters, which converts the value of a fields into an indexable
format (currently only string or array).
A converter is a simple class which implements the interface
``Massive\Bundle\SearchBundle\Search\Converter\ConverterInterface``. To add
the converter to the system you simply add a tag to your custom service.
.. code-block:: xml
There is also a possibility to convert a value to multiple other fields. This can be useful to index objects.
.. code-block:: php
public function convert($value)
{
$object = $this->repository->findOneBy(['id' => $value]);
return [
'value' => $value,
'fields' => [
new Field('id', $object->getId()),
new Field('name', $object->getName()),
new Field('description', $object->getDescription()),
],
];
}
Assuming the property name of above value is `foo`, this example would result in the following fields:
* ``foo``
* ``foo#id``
* ``foo#name``
* ``foo#description``
Reindex Providers
-----------------
When you implement a new driver you will most likely want to be able to
*re-index* objects which fall within the scope of this driver. In order to do
this you must create a class implementing ``ReindexProviderInterface`` and
add it to your service configutation with the ``massive_search.reindex.provider`` tag:
.. code-block:: xml
Events
------
The MassiveSearchBundle issues events which can be listened to by using the
standard Symfony event dispatcher. You can register a listener in your
dependency injection configuration as follows:
.. code-block:: xml
``massive_search.hit``
~~~~~~~~~~~~~~~~~~~~~~
The ``SearchManager`` will fire an event of type ``HitEvent`` in the Symfony EventDispatcher named
``massive_search.hit``.
The ``HitEvent`` contains the hit object and the reflection class of the
object which was originally indexed.
For example:
.. code-block:: php
getDocumentReflection();
if (false === $reflection->isSubclassOf('MyClass')) {
return;
}
$document = $event->getDocument();
$document->setUrl('Foo' . $document->getUrl());
}
}
``massive_search.pre_index``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fired before a document is indexed. See the code for more information.
``massive_search.search``
~~~~~~~~~~~~~~~~~~~~~~~~~
Fired when a search request is performed. See the code for more information.
.. JMS Metadata_: https://github.com/schmittjoh/metadata