2016-03-06 28 views
2

你好,我有一個問題..Elasticsearch foselastica庫和GROUPBY

我有我的彈性彎曲庫

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 

我的問題是..

我需要按列3獲取查詢組。我不知道如何做到這一點。

我會爲信息感激..

回答

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

0

是的,但這裏的問題。

如何讀取此數據? 我搜索這個,我發現methos搜索()。它用方法getAggregations()返回SetResults。

但..這是庫..有方法find()方法返回數組...

如何得到我聚集在這種情況下?

+0

我添加了循環數據存儲區的示例 –

+0

ResultSet是可迭代的,因此您可以循環訪問它。 –