【问题标题】:Elasticsearch foselastica repository and groupByElasticsearch foselastica 存储库和 groupBy
【发布时间】:2016-03-06 20:42:58
【问题描述】:

你好,我有问题..

我有我的 elastica 存储库

namespace XX\xxx;

use FOS\ElasticaBundle\Repository;

class TestRepository extends Repository
{
public function getExamples($argOne, $argTwo) {
    $query = new BoolQuery();

    $matchOne = new Match();
    $matchOne->setField('column_one', $argOne);
    $query->addMust($matchOne);

    $matchTwo = new Match();
    $matchOne->setField('column_two', $argTwo);
    $query->addMust($matchTwo);

    return $this->find($query);
  }
}

和映射

...
types:
   example:
      mappings:
         column_one:
              type: integer
         column_two:
              type: string
         column_three:
              type: date

我的问题是……

我需要按第三列获取查询组。我不知道该怎么做。

我会很感激你的信息..

【问题讨论】:

    标签: symfony elasticsearch elastica foselasticabundle


    【解决方案1】:

    您需要使用Aggregations

    例子:

    use Elastica\Aggregation\Terms;
    use Elastica\Query;
    
    // set up the aggregation
    $termsAgg = new Terms("dates");
    $termsAgg->setField("column_three");
    $termsAgg->setSize(10);
    
    // add the aggregation to a Query object
    $query = new Query();
    $query->addAggregation($termsAgg);
    
    $index = $elasticaClient->getIndex('someindex');
    $buckets = $index->search($query)->getAggregation("dates");
    
    foreach($buckets as $bucket){
        $statsAggResult = $bucket["column_three"];
        // do something with the result of the stats agg
    }
    

    在这里阅读更多:http://elastica.io/example/aggregations/terms.html

    【讨论】:

      【解决方案2】:

      是的,但这里有问题。

      如何读取这些数据? 我对此进行了搜索,发现了methos search()。它使用 getAggregations() 方法返回 SetResults。

      但是.. 这是存储库.. 有方法 find() 返回数组...

      在这种情况下如何获取 I 聚合?

      【讨论】:

      • 我添加了循环遍历数据桶的示例
      • ResultSet 是可迭代的,因此您可以循环遍历它。
      【解决方案3】:

      术语聚合

      来自https://elastica.io/example/aggregations/terms.html

      use Elastica\Aggregation\Terms;
      use Elastica\Aggregation\Stats;
      use Elastica\Query;
      
      $termsAgg = new Terms("genders");
      $termsAgg->setField("gender");
      $termsAgg->setOrder("height_stats.avg", "desc");
      
      $statsAgg = new Stats("height_stats");
      $statsAgg->setField("height");
      
      $termsAgg->addAggregation($statsAgg);
      
      $index = $elasticaClient->getIndex('someindex');
      $buckets = $index->search($query)->getAggregation("genders");
      
      foreach($buckets as $bucket){
          $statsAggResult = $bucket["height_stats"];
          // do something with the result of the stats agg
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-20
        • 1970-01-01
        • 2017-12-10
        • 2017-09-19
        • 2020-11-14
        • 2021-09-18
        • 1970-01-01
        • 2013-02-23
        • 2022-01-23
        相关资源
        最近更新 更多