Search code examples
phpsymfonyelasticsearchfoselasticabundle

Defining an ElasticSearch mapping for a particular entity with FOSElastica


I'm currently trying to use FOSElastica and I'm having trouble with the configuration. I want to use a serializer with most of my entities, but I want to specify the mapping for a particular entity, how could I do this ?

I've tried defining the "properties" options but with the "serializer" option defined it just seems to ignore it.

I'd want to do something like this, but the "table_content" properties is just ignored.

fos_elastica:
    serializer: 
        serializer: jms_serializer
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        table_content: 
            types: 
                table_content: 
                    properties:
                        id: ~
                    persistence: 
                        driver: orm 
                        model: TAMAS\AstroBundle\Entity\TableContent
        astonomical_object:
            types:
                astonomical_object:
                    serializer:
                        groups: [astonomicalobject]
                        serialize_null: true
                    persistence:
                        driver: orm
                        model: TAMAS\AstroBundle\Entity\AstronomicalObject

Solution

  • So, in fact FOSElastica does indeed recognize the type when you map it by hand in the config file, it was a bad understanding on my part. But it was still tedious since I had objects within objects within objects...

    So, after some time I found a solution to my problems : Dynamic templates and Index templates

    I actually had trouble with ElasticSearch not recognizing some types of fields (like date or geo_point), so I forced them for specifically named fields with help of templates.

    If you want an example of my configuration in FOSElastica (doc is here) :

    fos_elastica:
        serializer: 
            serializer: jms_serializer
        clients:
            default: 
                host: localhost 
                port: 9200
        index_templates: # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/indices-templates.html
            base_template: # this is a custom name for the index template
                client: default
                template: "*" # this is where you define which indices will use this template
                types:
                    _doc: # this is where you define which types will use this (_doc stands for every type/documents)
                        dynamic_templates: # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/dynamic-templates.html
                            dynamic_date_template: # this is a custom name for the dynamic field template
                                match_pattern: regex
                                match: created|updated|tpq_date|taq_date
                                mapping:
                                    type: date
                            dynamic_location_template:
                                match: location
                                mapping:
                                    type: geo_point