Search code examples
symfonyelasticsearchelasticafoselasticabundle

FOSElasticaBundle: Setting analyzer for custom properties


I am using the FOSElasticaBundle in Symfony 3.3. I have registered an event listener on the POST_TRANSFORM event that computes and adds a custom property like this:

public function addCustomProperty(TransformEvent $event)
{
    $document = $event->getDocument();
    $custom = $this->anotherService->calculateCustom($event->getObject());

    $document->set('custom', $custom);
}

Now I need to set the analyzer to be used for this property. How can I do that?

I already tried to add the custom field name to the type definition in my fos_elastica config but that causes an exception as the bundle then expects that property on my entity as well.


Solution

  • I finally found out that I could use dynamic_templates to set the desired analyzer like this:

    fos_elastica:
      indexes:
        app:
          types:
            myentity:
              dynamic_templates:
                custom_field:
                  path_match: customfield.*
                  mapping:
                    analyzer: myanalyzer
              properties:
                ...