如何在Symfony2:Fos ElasticaBundle上使用日期(Datehistogram)进行构面

时间:2016-04-14 16:53:41

标签: symfony elasticsearch twig

如何在symfony2上的elasticsearch中使用日期进行facet和aggreagtion查询?

感谢您的帮助!

雨果。

我需要得到这样的结果:按年计算服务器总数。

这是我的代码但是,我没有结果或在我的Twig中我不知道如何查看结果。

感谢。

雨果

    $search = $this->get('fos_elastica.index.serveurs.serveur');

    $queryServeurs = new \Elastica\Query\MatchAll();

    $elasticaQueryServeurs = new \Elastica\Query();
    $elasticaQueryServeurs->setQuery($queryServeurs);
    $elasticaQueryServeurs->setSize(0);

    $elasticaAggregDateServeur = new \Elastica\Aggregation\DateHistogram('dateHistogram','dateGarantie','year');
    $elasticaAggregDateServeur->setFormat("dd-MM-YYYY");

    $typesServeursaggregation = new \Elastica\Aggregation\Terms('types');
    $typesServeursaggregation->setField("type");
    $elasticaAggregDateServeur->addAggregation($typesServeursaggregation);

    $elasticaQueryServeurs->addAggregation($elasticaAggregDateServeur);

    $resultsServeursDate[] =$elasticaQueryServeurs;

return $this->container->get('templating')->renderResponse('VefsiffReferentielsBundle:stats:stats.html.twig', array( 'serveursdates' => $resultsServeursDate ));

$search = $this->get('fos_elastica.index.serveurs.serveur'); $queryServeurs = new \Elastica\Query\MatchAll(); $elasticaQueryServeurs = new \Elastica\Query(); $elasticaQueryServeurs->setQuery($queryServeurs); $elasticaQueryServeurs->setSize(0); $elasticaAggregDateServeur = new \Elastica\Aggregation\DateHistogram('dateHistogram','dateGarantie','year'); $elasticaAggregDateServeur->setFormat("dd-MM-YYYY"); $typesServeursaggregation = new \Elastica\Aggregation\Terms('types'); $typesServeursaggregation->setField("type"); $elasticaAggregDateServeur->addAggregation($typesServeursaggregation); $elasticaQueryServeurs->addAggregation($elasticaAggregDateServeur); $resultsServeursDate[] =$elasticaQueryServeurs;

1 个答案:

答案 0 :(得分:0)

我发布了一个新的聚合代码,其中包含了服务日期。 使用DateHistogram的一个条件:“dateGarantie”字段必须是文件config.yml中的Date类型。

提取到文件config.yml:

dateGarantie: {type: date}

这里解压缩到文件控制器:

$search = $this->get('fos_elastica.index.serveurs.serveur');

    $queryServeurs = new \Elastica\Query\MatchAll();

    $elasticaQueryServeurs = new \Elastica\Query();
    $elasticaQueryServeurs->setQuery($queryServeurs);
    $elasticaQueryServeurs->setSize(0);

    $elasticaAggregDateServeur = new \Elastica\Aggregation\DateHistogram('dateHistogram','dateGarantie','year');
    $elasticaAggregDateServeur->setFormat("YYYY");

    $elasticaQueryServeurs->addAggregation($elasticaAggregDateServeur);
    $resultsDateServeurs = $search->search($elasticaQueryServeurs);

return $this->container->get('templating')->renderResponse('refBundle:stats:stats.html.twig', array(
                                        'serveursdates' => $resultsDateServeurs->getAggregations()));

最后这是一个树枝的代码。我使用Morris的代码来获得漂亮的图表:

<div class="col-sm-8">
                         <h3>Serveurs à changer</h3>
                          <style>
                          #serveur2 {
                          width: 800px;
                          height: 300px;
                          margin: auto 0 auto;
                          </style>
                         <div id="serveur2"></div>

                         <script>
                         // Use Morris.Bar
                            Morris.Bar({
                              element: 'serveur2',
                              data: [ {% for serveursdate in serveursdates.dateHistogram.buckets %}
                                {x: '{{serveursdate.key_as_string}}' ,Total: {{ serveursdate.doc_count }} },

                              {% endfor %} ],
                              xkey: 'x',
                              ykeys: [ 'Total'],
                              labels: ['Total Serveur'],
                              barColors: function (row, series, type)
                                            {
                                                      if (type === 'bar')
                                                      {
                                                        var red = Math.ceil(255 * row.y /this.ymax);
                                                        return 'rgb(' + red + ',0,0)';
                                                      }
                                                      else {
                                                        return '#000';
                                                      }
                                            }

                            });
                         </script>


                         <hr>
            </div