addAggregation() публичный Метод

Adds an Aggregation to the query.
public addAggregation ( AbstractAggregation $agg )
$agg Elastica\Aggregation\AbstractAggregation
Пример #1
1
 public function indexAction(Request $request, $page = 1)
 {
     $getTypes = $request->query->get('type_filters');
     $getSubjects = $request->query->get('subject_filters');
     $getPublishers = $request->query->get('publisher_filters');
     $typeFilters = !empty($getTypes) ? explode(',', $getTypes) : [];
     $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
     $publisherFilters = !empty($getPublishers) ? explode(',', $getPublishers) : [];
     $journalSearcher = $this->get('fos_elastica.index.search.journal');
     $boolQuery = new Query\Bool();
     $match = new Query\Match();
     $match->setField('status', 1);
     $boolQuery->addMust($match);
     $match = new Query\Match();
     $match->setField('published', true);
     $boolQuery->addMust($match);
     if (!empty($typeFilters) || !empty($subjectFilters) || !empty($publisherFilters)) {
         foreach ($typeFilters as $type) {
             $match = new Query\Match();
             $match->setField('publisher.publisher_type.name', $type);
             $boolQuery->addMust($match);
         }
         foreach ($subjectFilters as $subject) {
             $match = new Query\Match();
             $match->setField('subjects.subject', $subject);
             $boolQuery->addMust($match);
         }
         foreach ($publisherFilters as $publisher) {
             $match = new Query\Match();
             $match->setField('publisher.name', $publisher);
             $boolQuery->addMust($match);
         }
     }
     $journalQuery = new Query($boolQuery);
     $typeAgg = new Aggregation\Terms('types');
     $typeAgg->setField('publisher.publisher_type.name');
     $typeAgg->setOrder('_term', 'asc');
     $typeAgg->setSize(0);
     $journalQuery->addAggregation($typeAgg);
     $subjectAgg = new Aggregation\Terms('subjects');
     $subjectAgg->setField('subjects.subject');
     $subjectAgg->setOrder('_term', 'asc');
     $subjectAgg->setSize(0);
     $journalQuery->addAggregation($subjectAgg);
     $publisherAgg = new Aggregation\Terms('publishers');
     $publisherAgg->setField('publisher.name');
     $publisherAgg->setOrder('_term', 'asc');
     $publisherAgg->setSize(0);
     $journalQuery->addAggregation($publisherAgg);
     $adapter = new ElasticaAdapter($journalSearcher, $journalQuery);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage(21);
     $pagerfanta->setCurrentPage($page);
     $journals = $pagerfanta->getCurrentPageResults();
     $types = $adapter->getResultSet()->getAggregation('types')['buckets'];
     $subjects = $adapter->getResultSet()->getAggregation('subjects')['buckets'];
     $publishers = $adapter->getResultSet()->getAggregation('publishers')['buckets'];
     $data = ['types' => $types, 'subjects' => $subjects, 'publishers' => $publishers, 'type_filters' => $typeFilters, 'subject_filters' => $subjectFilters, 'publisher_filters' => $publisherFilters, 'journals' => $journals, 'pagerfanta' => $pagerfanta, 'page' => 'explore'];
     return $this->render('OjsSiteBundle:Explore:index.html.twig', $data);
 }
Пример #2
1
 /**
  * @group functional
  */
 public function testReverseNestedAggregation()
 {
     $agg = new Nested('comments', 'comments');
     $names = new Terms('name');
     $names->setField('comments.name');
     $tags = new Terms('tags');
     $tags->setField('tags');
     $reverseNested = new ReverseNested('main');
     $reverseNested->addAggregation($tags);
     $names->addAggregation($reverseNested);
     $agg->addAggregation($names);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('comments');
     $this->assertArrayHasKey('name', $results);
     $nameResults = $results['name'];
     $this->assertCount(3, $nameResults['buckets']);
     // bob
     $this->assertEquals('bob', $nameResults['buckets'][0]['key']);
     $tags = array(array('key' => 'foo', 'doc_count' => 2), array('key' => 'bar', 'doc_count' => 1), array('key' => 'baz', 'doc_count' => 1));
     $this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']);
     // john
     $this->assertEquals('john', $nameResults['buckets'][1]['key']);
     $tags = array(array('key' => 'bar', 'doc_count' => 1), array('key' => 'foo', 'doc_count' => 1));
     $this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']);
     // susan
     $this->assertEquals('susan', $nameResults['buckets'][2]['key']);
     $tags = array(array('key' => 'baz', 'doc_count' => 1), array('key' => 'foo', 'doc_count' => 1));
     $this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']);
 }
Пример #3
0
 /**
  * @param  Request $request
  * @param  int $page
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function indexAction(Request $request, $page = 1)
 {
     $getRoles = $request->query->get('role_filters');
     $getSubjects = $request->query->get('subject_filters');
     $getJournals = $request->query->get('journal_filters');
     $roleFilters = !empty($getRoles) ? explode(',', $getRoles) : [];
     $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : [];
     $journalFilters = !empty($getJournals) ? explode(',', $getJournals) : [];
     $userSearcher = $this->get('fos_elastica.index.search.user');
     $userQuery = new Query('*');
     if (!empty($roleFilters) || !empty($subjectFilters) || !empty($journalFilters)) {
         $boolQuery = new Query\Bool();
         foreach ($roleFilters as $role) {
             $match = new Query\Match();
             $match->setField('journalUsers.roles', $role);
             $boolQuery->addMust($match);
         }
         foreach ($subjectFilters as $subject) {
             $match = new Query\Match();
             $match->setField('subjects', $subject);
             $boolQuery->addMust($match);
         }
         foreach ($journalFilters as $journal) {
             $match = new Query\Match();
             $match->setField('journalUsers.journal.title', $journal);
             $boolQuery->addMust($match);
         }
         $userQuery->setQuery($boolQuery);
     }
     $roleAgg = new Aggregation\Terms('roles');
     $roleAgg->setField('journalUsers.roles');
     $roleAgg->setOrder('_term', 'asc');
     $roleAgg->setSize(0);
     $userQuery->addAggregation($roleAgg);
     $subjectAgg = new Aggregation\Terms('subjects');
     $subjectAgg->setField('subjects');
     $subjectAgg->setOrder('_term', 'asc');
     $subjectAgg->setSize(0);
     $userQuery->addAggregation($subjectAgg);
     $journalAgg = new Aggregation\Terms('journals');
     $journalAgg->setField('journalUsers.journal.title');
     $journalAgg->setOrder('_term', 'asc');
     $journalAgg->setSize(0);
     $userQuery->addAggregation($journalAgg);
     $adapter = new ElasticaAdapter($userSearcher, $userQuery);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setMaxPerPage(20);
     $pagerfanta->setCurrentPage($page);
     $people = $pagerfanta->getCurrentPageResults();
     $roles = $adapter->getResultSet()->getAggregation('roles')['buckets'];
     $subjects = $adapter->getResultSet()->getAggregation('subjects')['buckets'];
     $journals = $adapter->getResultSet()->getAggregation('journals')['buckets'];
     $data = ['people' => $people, 'roles' => $roles, 'subjects' => $subjects, 'journals' => $journals, 'pagerfanta' => $pagerfanta, 'role_filters' => $roleFilters, 'subject_filters' => $subjectFilters, 'journal_filters' => $journalFilters, 'page' => 'ojs_site_people_index'];
     return $this->render('OjsSiteBundle:People:index.html.twig', $data);
 }
Пример #4
0
 protected function getStates()
 {
     $results = [];
     $query = new Elastica\Query();
     $query->setSize(0);
     $agg = new Elastica\Aggregation\Nested('states', 'state');
     $st_terms = new Elastica\Aggregation\Terms('abbrev');
     $st_terms->setField('state.abbrev');
     $st_terms->setOrder('_term', 'asc');
     $st_terms->setSize(0);
     $state_terms = new Elastica\Aggregation\Terms('full');
     $state_terms->setField('state.full');
     $st_terms->addAggregation($state_terms);
     $agg->addAggregation($st_terms);
     $query->addAggregation($agg);
     /* @var $elastica_client Elastica\Client */
     $elastica_client = $this->getServiceLocator()->getServiceLocator()->get('elastica-client');
     try {
         /* @var $response \Elastica\Response */
         $response = $elastica_client->request('usgeodb/locality/_search?query_cache=true', Request::GET, $query->toArray());
         $data = $response->getData();
         $aggregations = isset($data['aggregations']['states']['abbrev']['buckets']) ? $data['aggregations']['states']['abbrev']['buckets'] : [];
         foreach ($aggregations as $aggregation) {
             $key = strtoupper($aggregation['key']);
             $value = ucwords($aggregation['full']['buckets'][0]['key']);
             $results[$key] = $value;
         }
     } catch (\Exception $e) {
     }
     return $results;
 }
Пример #5
0
 private function getStatsQuery(Journal $journal)
 {
     $filter = new Term();
     $filter->setTerm('journal.id', $journal->getId());
     $filterQuery = new Filtered();
     $filterQuery->setFilter($filter);
     $query = new Query($filterQuery);
     $dateHistogram = new DateHistogram('dateHistogram', 'created', 'month');
     $dateHistogram->setFormat("YYYY-MM-dd");
     $query->addAggregation($dateHistogram);
     $genderAggregation = new Terms('language');
     $genderAggregation->setField('locale');
     $query->addAggregation($genderAggregation);
     $query->setSize(0);
     return $query;
 }
Пример #6
0
 /**
  * @group functional
  */
 public function testValueCountAggregation()
 {
     $agg = new ValueCount('count', 'price');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('count');
     $this->assertEquals(5, $results['value']);
 }
 /**
  * @group functional
  */
 public function testScriptedMetricAggregation()
 {
     $agg = new ScriptedMetric('scripted', "_agg['durations'] = [:]", "key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;", 'values = []; for (item in _agg.durations) { values.add(item.value) }; return values');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('scripted');
     $this->assertEquals(array(100, 50, 150), $results['value'][0]);
 }
 public function testDateHistogramAggregation()
 {
     $agg = new DateHistogram("hist", "created", "1h");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("hist");
     $this->assertEquals(3, sizeof($results['buckets']));
 }
Пример #9
0
 /**
  * @group functional
  */
 public function testMissingAggregation()
 {
     $agg = new Missing('missing', 'color');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('missing');
     $this->assertEquals(1, $results['doc_count']);
 }
 public function testValueCountAggregation()
 {
     $agg = new ValueCount("count", "price");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("count");
     $this->assertEquals(5, $results['value']);
 }
 public function testMissingAggregation()
 {
     $agg = new Missing("missing", "color");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("missing");
     $this->assertEquals(1, $results['doc_count']);
 }
 public function testCardinalityAggregation()
 {
     $agg = new Cardinality("cardinality");
     $agg->setField("color");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("cardinality");
     $this->assertEquals(3, $results['value']);
 }
 public function testSumAggregation()
 {
     $agg = new Sum("sum");
     $agg->setField("price");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("sum");
     $this->assertEquals(5 + 8 + 1 + 3, $results['value']);
 }
Пример #14
0
 /**
  * @group functional
  */
 public function testSetOffsetWorks()
 {
     $agg = new DateHistogram('hist', 'created', '1m');
     $agg->setOffset('+40s');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
     $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
 }
 public function testMinAggregation()
 {
     $agg = new Min("min_price");
     $agg->setField("price");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("min_price");
     $this->assertEquals(1, $results['value']);
 }
Пример #16
0
 /**
  * @group functional
  */
 public function testSumAggregation()
 {
     $agg = new Sum('sum');
     $agg->setField('price');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('sum');
     $this->assertEquals(5 + 8 + 1 + 3, $results['value']);
 }
Пример #17
0
 /**
  * @group functional
  */
 public function testAvgAggregation()
 {
     $agg = new Avg('avg');
     $agg->setField('price');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregations();
     $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']['value']);
 }
Пример #18
0
 /**
  * @group functional
  */
 public function testCardinalityAggregation()
 {
     $agg = new Cardinality('cardinality');
     $agg->setField('color');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality');
     $this->assertEquals(3, $results['value']);
 }
Пример #19
0
 /**
  * @group functional
  */
 public function testAggregationScriptAsString()
 {
     $agg = new Sum('sum');
     $agg->setScript("doc['price'].value");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('sum');
     $this->assertEquals(5 + 8 + 1 + 3, $results['value']);
 }
 public function testGeoDistanceAggregation()
 {
     $agg = new GeoDistance("geo", "location", array("lat" => 32.804654, "lon" => -117.242594));
     $agg->addRange(null, 100);
     $agg->setUnit("mi");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("geo");
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
 }
 public function testTermsAggregation()
 {
     $agg = new Terms("terms");
     $agg->setField("color");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("terms");
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
     $this->assertEquals("blue", $results['buckets'][0]['key']);
 }
 public function testGeohashGridAggregation()
 {
     $agg = new GeohashGrid("hash", "location");
     $agg->setPrecision(3);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("hash");
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
     $this->assertEquals(1, $results['buckets'][1]['doc_count']);
 }
 public function testFilterNoSubAggregation()
 {
     $agg = new Avg("price");
     $agg->setField("price");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("price");
     $results = $results['value'];
     $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results);
 }
Пример #24
0
 /**
  * @group functional
  */
 public function testGeoDistanceAggregation()
 {
     $agg = new GeoDistance('geo', 'location', array('lat' => 32.804654, 'lon' => -117.242594));
     $agg->addRange(null, 100);
     $agg->setUnit('mi');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('geo');
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
 }
 /**
  * @group functional
  */
 public function testRangeAggregation()
 {
     $agg = new Range('range');
     $agg->setField('price');
     $agg->addRange(1.5, 5);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('range');
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
 }
Пример #26
0
 /**
  * @group functional
  */
 public function testTermsAggregation()
 {
     $agg = new Terms('terms');
     $agg->setField('color');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('terms');
     $this->assertEquals(2, $results['buckets'][0]['doc_count']);
     $this->assertEquals('blue', $results['buckets'][0]['key']);
 }
 public function testNestedAggregation()
 {
     $agg = new Nested("resellers", "resellers");
     $min = new Min("min_price");
     $min->setField("price");
     $agg->addAggregation($min);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("resellers");
     $this->assertEquals(4.98, $results['min_price']['value']);
 }
Пример #28
0
 /**
  * @group functional
  */
 public function testDateRangeSetFormat()
 {
     $agg = new DateRange('date');
     $agg->setField('created');
     $agg->addRange(1390958535000)->addRange(null, 1390958535000);
     $agg->setFormat('m-y-d');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('date');
     $this->assertEquals('22-2014-29', $results['buckets'][0]['to_as_string']);
 }
Пример #29
0
 /**
  * @group functional
  */
 public function testNestedAggregation()
 {
     $agg = new Nested('resellers', 'resellers');
     $min = new Min('min_price');
     $min->setField('resellers.price');
     $agg->addAggregation($min);
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('resellers');
     $this->assertEquals(4.98, $results['min_price']['value']);
 }
Пример #30
0
 /**
  * @group functional
  */
 public function testHistogramAggregation()
 {
     $agg = new Histogram('hist', 'price', 10);
     $agg->setMinimumDocumentCount(0);
     // should return empty buckets
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
     $buckets = $results['buckets'];
     $this->assertEquals(5, sizeof($buckets));
     $this->assertEquals(30, $buckets[3]['key']);
     $this->assertEquals(2, $buckets[3]['doc_count']);
 }