Skip to content

Search Response Listener, add additional info #257

Open
@khusseini

Description

@khusseini

Hello there,

I was trying to find a "nice" way to enrich a document with additional data from other data sources, so the first spot to look at was the ongr_filter_manager.search_response event.

  SearchBundle\EventListener\EsSearchResponseListener:
    arguments:
    - '@search.indexer.object_repository'
    tags:
    - name: kernel.event_listener
      event: ongr_filter_manager.search_response
class EsSearchResponseListener
{
    /** @var SearchIndexerInterface */
    private $indexer;

    public function __construct(SearchIndexerInterface $indexer)
    {
        $this->indexer = $indexer;
    }

    public function onOngrfiltermanagerSearchresponse(SearchResponseEvent $event)
    {
        $documents = $event->getDocumentIterator();
        $config = $this->indexer->getConfig();

        foreach ($documents as $document) {
            if (!$document instanceof TypedContent) {
                continue;
            }

            if (
                !in_array(get_class($document), array_keys($config))
                || !in_array($document->getType(), array_keys($config[get_class($document)]))
            ) {
                continue;
            }

            $conf = $config[get_class($document)][$document->getType()];

            /** @var ObjectRepository $repository */
            $repository = $conf['repository'];
            $obj = $repository->findOneBy([
                $config['identifier'] => $document->getId()
            ]);

            $document->setDataObject($obj);
        }
    }
}

it was only after I wrote this, that I realized that the entries for the the results are not changed outside of the listeners scope.
What would be a good way to achieve something like this?
Are there maybe any events fired when the results are converted from their raw form to the document object?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions