2

我有一个“文章”实体,它与实体“率”有一对多的关系。

我的文章已经被索引了。我想将文章的平均费率添加到索引中(使用与文章相关的“费率”实体计算),我不知道如何执行此操作,如果新费率是,则必须更新平均费率创建的。

对于 config.yml 中的映射:

indexes:
    piy:
        client: default
        settings:
            index:
                analysis:
                  analyzer:
                    custom_search_analyzer:
                        type: custom
                        tokenizer: standard
                        filter   : [standard, lowercase, asciifolding]
                    custom_index_analyzer:
                        type: custom
                        tokenizer: standard
                        filter   : [standard, lowercase, asciifolding, custom_filter]
                  filter:
                    custom_filter:
                        type: edgeNGram
                        side: front
                        min_gram: 1
                        max_gram: 20
        types:
            article:
                mappings:
                    title : { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                    user:
                      type : object
                      properties : 
                        fullName : { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                persistence:
                    driver: orm
                    model: Piy\CoreBundle\Entity\Article
                    elastica_to_model_transformer:
                      service: piy.transformers.elastica.article
                    finder: ~
                    provider: ~
                    listener: ~            

对于速率实体映射:

/**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer", length=1)
     */
    private $value;


    /**
     * @ORM\ManyToOne(targetEntity="Piy\CoreBundle\Entity\User", inversedBy="articleRates")
    */
    public $user; 

    /**
     * @ORM\ManyToOne(targetEntity="Piy\CoreBundle\Entity\Article", inversedBy="rates")
     */
    private $article;
4

2 回答 2

1

通过添加过滤器脚本,我找到了一种方法:

$rateFilter = new \Elastica\Filter\Script("sum=0; foreach( rate : doc['value'].values) { sum = sum + rate }; avg = sum/doc['value'].values.length; avg >= ".$searchRate.";  ");

但是现在我有另一个问题:当我在一篇文章上添加一个新的价格时,它没有被添加到索引中......

编辑: 要在添加或更新速率时更新文章父级,我在 postPersist 和 postUpdate 上添加了一个监听器,用于启动文章的 postUpdate,并更新索引

于 2014-05-15T09:50:13.570 回答
0

您需要更新相关对象(速率实体)的索引映射,如下所示

...
    types:
        article:
            mappings:
                title : { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                user:
                  type : object
                  properties : 
                    fullName : { search_analyzer: custom_search_analyzer, index_analyzer: custom_index_analyzer, type: string }
                rates:
                   type : object
                   properties :
                      id : ~
                      value: ~

现在它将索引文章和与文章相关的值(如果没有为文章设置值,它将索引文章但没有值)

于 2014-05-14T16:27:00.807 回答