FriendsOfSymfony / FOSElasticaBundle

Elasticsearch PHP integration for your Symfony project using Elastica.

Home Page:http://friendsofsymfony.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Howto transform complete entity?

mmaedler opened this issue · comments

Hi,

first of all thank you so much for providing this awesome package! It made integrating our service with elastic a breeze!

However, at the moment I am looking for an elegant solution storing slightly modified copies of my entity in elastic search without having to define all the properties by hand.

But first some context: I have a rest service that provides product information. The main entity I am dealing with, is Product which in itself consists of numerous other related entites. Since these Product entities tend to be rather big in the end, I have decided not to return all of it as part of the response to requests to the service. Besides that I have created a DTO that contains both a subset of data that I have in the entity as well as some "computed" values based on what I have stored in other fields within the entity. Upon request the entity gets loaded, send through a custom converter class that maps the entity values to the one in the dto. Then the DTO gets serialized with JMS and returned as response. This logic already existed before I introduced Elastic (and fosElasticaBundle) to the stack.

What I want to do now is to apply the logic (entity -> converter -> model) before storing anything in elastic. This would speed up requests, since I can return the result completely from elastic. Question is: how can I hook into the ModelToElastic Transformation process provided by fosElasticaBundle and swap out the entity for the DTO and leverage my already existing serialization information before sending things off to Elastic?

Here's my current foselastica config for reference:

fos_elastica:
    clients:
        default: { url: '%env(ELASTICSEARCH_URL)%' }
    serializer:
        serializer: jms_serializer
#        callback_class: '@app.serializer_callback.product'
    indexes:
        product:
            index_name: '%env(ELASTICSEARCH_PRODUCT_BASE_INDEX)%'
            settings:
                index:
                    max_result_window: 200000
            persistence:
                driver: orm
                model: App\Entity\Product
                provider:
                  batch_size: 50
                finder: ~
#                model_to_elastica_transformer:
#                    service: app.model_to_elastica_transformer.product
            serializer:
                serialize_null: true

Up to now I tried two things:

  1. Custom modelToElasticaTransformer -> no success
  2. Custom SerializerCallback -> no success

How would you tackle this? Thanks!