Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch foselastica repository and groupBy

Hello I have a problem..

I have my elastica repository

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);
  }
}

And mapping

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

My problem is..

I need to get query group by column three. And I have no idea how to do this.

I'll be grateful for the informations..

like image 602
Jack Red Avatar asked Mar 06 '16 20:03

Jack Red


1 Answers

You need to use Aggregations.

Example:

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
}

Read more here: http://elastica.io/example/aggregations/terms.html

like image 200
Paweł Mikołajczuk Avatar answered Oct 23 '22 03:10

Paweł Mikołajczuk