2

I am trying to setup a parent child relationship and I can't seem to get the mapping config right. I'm using Symfony2.5, FOSElastica 3.0.9 and Elasticsearch 1.4.4

Here is the relevent section from my mapping:

# fos elastica
fos_elastica:
    clients:
        default: { host: 127.0.0.1, port: 9200 }
    serializer: 
        callback_class: FOS\ElasticaBundle\Serializer\Callback
        serializer: serializer
    indexes:
        index:
            index_name: index_%kernel.environment%
            client: default
            types:
                company:
                    mappings:
                        id: 
                            type: "long"
                        company_name: 
                            type: "string"
                            fields:
                                raw:
                                    type: "string"
                                    index: "analyzed"
                                    index_analyzer: "sortable"
                    persistence:
                        elastica_to_model_transformer:
                            ignore_missing: true
                        driver: orm # orm, mongodb, propel are available
                        model: Alpha\RMSBundle\Entity\Company
                        provider: ~
                        finder: ~
                        listener: ~
                    serializer: 
                        groups: [company]
                job:
                    mappings:
                        id:
                            type: "long"
                        company:
                            type: "object"
                            properties:
                                id:
                                    type: "long"
                    _parent:
                        type: company
                        property: company
                        identifier: id
                    #_routing:
                    #    required: true
                    #    path: company
                    persistence:
                        elastica_to_model_transformer:
                            ignore_missing: true
                        driver: orm # orm, mongodb, propel are available
                        model: Alpha\RMSBundle\Entity\JobOpening
                        provider: ~
                        finder: ~
                        listener: ~
                    serializer: 
                        groups: [job]

First I populate company, but when I try to populate job I get the following error:

    [Elastica\Exception\ResponseException]                                 
  RoutingMissingException[routing is required for [index_v2]/[job]/[1]]

I have tried specifying the routing which is commented out above, but that didn't set the relationship either, I have taken the routing out as it is not mentioned in the docs. Can anyone see where I am going wrong?

Ben Stinton
  • 411
  • 2
  • 8
  • 17

2 Answers2

2

There is no problem with the mapping above, and _routing is not required. However FOSElastica will not populate the mapping with a _parent set.

However, as the docs says you need to to use setParent on the Document. In order to do this I need to setup a custom ModelToElasticaTransformer. This can be done by extending the ModelToElasticaAutoTransformer provided from the bundle in a class Alpha\RMSBundle\Transformer\JobToElasticaTransformer

In the transform() function I inserted a line:

$document->setParent($object->getCompany()->getId());

just above

return $document

Then you declare the service:

alpha.transformers.model.job:
    class:  Alpha\RMSBundle\Transformer\JobToElasticaTransformer
        calls:
            - [ setPropertyAccessor, ['@fos_elastica.property_accessor'] ]

Finally add the following in the mapping for the type in the persistence section:

model_to_elastica_transformer:
    service: alpha.transformers.model.job

Now you can use populate as normal, as long as you remember to populate the parent class first

Ben Stinton
  • 411
  • 2
  • 8
  • 17
1

The bundle explains this very clearly in it's documentation

Note that to create a document with a parent, you need to call setParent on the document rather than setting a _parent field. If you do this wrong, you will see a RoutingMissingException as Elasticsearch does not know where to store a document that should have a parent but does not specify it.

Derick F
  • 2,749
  • 3
  • 20
  • 30
  • 3
    Not that clear for me, sorry. Does this mean I can't use the populate command as is to initially populate the index, and would have to create a custom command? I have read the docs over and over, but still couldn't see what I needed. – Ben Stinton Apr 12 '15 at 19:30