Skip to content

Search Response Listener, add additional info #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
khusseini opened this issue Aug 21, 2018 · 0 comments
Open

Search Response Listener, add additional info #257

khusseini opened this issue Aug 21, 2018 · 0 comments

Comments

@khusseini
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant