无法使用 FOSElasticaBundle 设置父子

Unable to set up Parent Child with FOSElasticaBundle

我正在尝试设置父子关系,但我似乎无法获得正确的映射配置。我正在使用 Symfony2.5、FOSElastica 3.0.9 和 Elasticsearch 1.4.4

这是我映射中的相关部分:

# 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]

首先我填充公司,但是当我尝试填充工作时出现以下错误:

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

我已经尝试指定上面注释掉的路由,但这也没有设置关系,我已经删除了路由,因为它在文档中没有提到。 谁能看出我哪里出错了?

捆绑包在 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.

上面的映射没有问题,不需要_routing。但是,FOSElastica 不会使用 _parent 集填充映射。

但是,正如 docs 所说,您需要在文档上使用 setParent。为此,我需要设置自定义 ModelToElasticaTransformer。这可以通过扩展 class Alpha\RMSBundle\Transformer\JobToElasticaTransformer

中的捆绑包提供的 ModelToElasticaAutoTransformer 来完成

在 transform() 函数中我插入了一行:

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

略高于

return $document

然后声明服务:

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

最后在持久性部分的类型的映射中添加以下内容:

model_to_elastica_transformer:
    service: alpha.transformers.model.job

现在您可以正常使用填充,只要您记得首先填充父级 class