public function getStatsQuery(ArticleSearch $articleSearch) { $query = $this->getQueryForSearch($articleSearch); //Simple aggregation (based on tags, we will get doc_count for each tag) $tagsAggregation = new \Elastica\Aggregation\Terms('tag'); $tagsAggregation->setField('tags'); //More complex aggregation. We will get categories for each month $dateAggregation = new \Elastica\Aggregation\DateHistogram('dateHistogram', 'publishedAt', 'month'); $dateAggregation->setFormat("dd-MM-YYYY"); $categoryAggregation = new \Elastica\Aggregation\Terms('category'); $categoryAggregation->setField("category"); $dateAggregation->addAggregation($categoryAggregation); $query->addAggregation($tagsAggregation); $query->addAggregation($dateAggregation); //We don't need results, just stats. $query->setSize(0); return $query; }
/** * @return \Elastica\Aggregation\Terms */ public function getAggregation() { $aggregation = new \Elastica\Aggregation\Terms($this->name); $aggregation->setField($this->field); $aggregation->setSize($this->size); return $aggregation; }
public function getArticles($user, $searchTerm, $page, $pageOffset, $category, $orderby, $colors, $finder, $elasticIndex) { $boolQuery = new \Elastica\Query\BoolQuery(); if ($category != NULL) { $query = $this->getEntityManager()->createQuery("SELECT c.id FROM OrthIndexBundle:Categories c WHERE c.id LIKE :category")->setParameter('category', $category . "%"); $queryResult = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); foreach ($queryResult as $categoryId) { $categoryArray[] = $categoryId['id']; } $categoryQuery = new \Elastica\Query\Terms(); $categoryQuery->setTerms('catRef', $categoryArray); $boolQuery->addMust($categoryQuery); } if ($searchTerm) { $fieldQuery = new \Elastica\Query\Match(); $fieldQuery->setFieldQuery('allField', $searchTerm); $fieldQuery->setFieldOperator('allField', 'AND'); $fieldQuery->setFieldMinimumShouldMatch('allField', '70%'); $fieldQuery->setFieldFuzziness('allField', '0.8'); $fieldQuery->setFieldAnalyzer('allField', 'custom_search_analyzer'); $boolQuery->addMust($fieldQuery); } if ($colors != NULL) { $colorQuery = new \Elastica\Query\Terms(); $colorQuery->setTerms('variants.variantvalues.otherTerms', $colors); $colorNested = new \Elastica\Query\Nested('variants'); $colorNested->setPath('variants.variantvalues'); $colorNested->setQuery($colorQuery); $boolQuery->addMust($colorNested); } $agg = new \Elastica\Aggregation\Terms("catRef"); $agg->setSize(5000); $agg->setField('catRef'); $boolFilter = new \Elastica\Filter\Bool(); if ($user == "anon.") { $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0))); } else { $boolFilter->addShould(new \Elastica\Filter\Terms('customized', array(0, $user->getCustomerRef()))); } $filtered = new \Elastica\Query\Filtered($boolQuery, $boolFilter); $query = new \Elastica\Query(); $query->setQuery($filtered); //if( $colors != NULL) { // $query->setFilter($colorNested); //} if ($orderby == 'desc') { $query->setSort(array('variants.price' => array('order' => 'desc'))); } elseif ($orderby == 'asc') { $query->setSort(array('variants.price' => array('order' => 'asc'))); } $query->addAggregation($agg); $query->setSize(12); $query->setFrom($pageOffset); $articles = $finder->find($query); $aggregations = $elasticIndex->search($query); $result = array("articles" => $articles, "aggs" => $aggregations, "rQuery" => $query); return $result; }
public function testAggregation() { $this->createData(); $searchManager = $this->getSearchManager(); $deviceAggregation = new \Elastica\Aggregation\Terms('id'); $deviceAggregation->setField('device'); $query = $searchManager->generateQueryBy([]); $aggregationResult = $searchManager->createQuery()->from(View::class)->searchWith($query)->addAggregation($deviceAggregation)->getResult(Query::HYDRATE_AGGREGATION); $expectedResult = array('id' => array('buckets' => array(0 => array('key' => 'ios', 'doc_count' => 3), 1 => array('key' => 'android', 'doc_count' => 1)))); $this->assertEquals($expectedResult, $aggregationResult); }
public function facet($filter, $sort = 'membership') { $boolQuery = new \Elastica\Query\Bool(); $queryStatus = new \Elastica\Query\Match(); $queryStatus->setFieldQuery('place.status', StatusType::VALIDATED); $boolQuery->addMust($queryStatus); if ($filter->getCategory()) { $queryCategory = new \Elastica\Query\Match(); $queryCategory->setFieldQuery('place.categories.slug', $filter->getCategory()->getSlug()); $boolQuery->addMust($queryCategory); } $queryCity = new \Elastica\Query\Match(); $queryCity->setFieldQuery('place.city.slug', $filter->getCity()->getSlug()); $boolQuery->addMust($queryCity); ##AGGREGATION - FACETED## $now = new \DateTime(); $this->addCollections($boolQuery, $filter); $boolFilter = new \Elastica\Filter\Bool(); //Filters $businessHoursDayFilter = new \Elastica\Filter\Term(['businessHours.day' . date('l') => true]); $businessHoursStartsAtFilter = new \Elastica\Filter\Range('businessHours.startsAtFormatted', array('lte' => $now->format('H:i:s'))); $businessHoursEndsAtFilter = new \Elastica\Filter\Range('businessHours.endsAtFormatted', array('gte' => $now->format('H:i:s'))); $businessHoursIs24HFilter = new \Elastica\Filter\Term(['is24h' => true]); $businessHoursExceptionDateFilter = new \Elastica\Filter\Term(['businessHoursException.dayFormatted' => date('Y-m-d')]); $businessHoursExceptionStartsAtFilter = new \Elastica\Filter\Range('businessHoursException.startsAtFormatted', array('lte' => $now->format('H:i:s'))); $businessHoursExceptionEndsAtFilter = new \Elastica\Filter\Range('businessHoursException.endsAtFormatted', array('gte' => $now->format('H:i:s'))); $businessHoursExceptionStartsAtMissingFilter = new \Elastica\Filter\Missing('businessHoursException.startsAtFormatted'); $businessHoursExceptionEndsAtMissingFilter = new \Elastica\Filter\Missing('businessHoursException.endsAtFormatted'); $businessHoursExceptionDateTimeFilter = new \Elastica\Filter\Bool(); $businessHoursExceptionDateTimeFilter->addMust($businessHoursExceptionDateFilter)->addMust($businessHoursExceptionStartsAtFilter)->addMust($businessHoursExceptionEndsAtFilter); $businessHoursExceptionAllDayClosedFilter = new \Elastica\Filter\Bool(); $businessHoursExceptionAllDayClosedFilter->addMust($businessHoursExceptionDateFilter)->addMust($businessHoursExceptionStartsAtMissingFilter)->addMust($businessHoursExceptionEndsAtMissingFilter); $businessHoursDayTimeFilter = new \Elastica\Filter\Bool(); $businessHoursDayTimeFilter->addMust($businessHoursDayFilter)->addMust($businessHoursStartsAtFilter)->addMust($businessHoursEndsAtFilter); #BusinessHours Filter $businessHoursFilter = new \Elastica\Filter\Bool(); $businessHoursFilter->addShould($businessHoursDayTimeFilter); #BusinessHoursException Filter $businessHoursExceptionFilter = new \Elastica\Filter\Bool(); $businessHoursExceptionFilter->addShould($businessHoursExceptionDateTimeFilter); $businessHoursNestedFilter = new \Elastica\Filter\Nested(); $businessHoursNestedFilter->setFilter($businessHoursFilter)->setPath('place.businessHours'); $businessHoursExceptionNestedFilter = new \Elastica\Filter\Nested(); $businessHoursExceptionNestedFilter->setFilter($businessHoursExceptionFilter)->setPath('place.businessHoursException'); $businessHoursExceptionMissingNestedFilter = new \Elastica\Filter\Nested(); $businessHoursExceptionMissingNestedFilter->setFilter($businessHoursExceptionAllDayClosedFilter)->setPath('place.businessHoursException'); $workingNowFilter = new \Elastica\Filter\Bool(); $workingNowFilter->addShould($businessHoursNestedFilter)->addShould($businessHoursExceptionNestedFilter)->addShould($businessHoursIs24HFilter)->addMustNot($businessHoursExceptionMissingNestedFilter); if ($filter->getBusinessHours()) { foreach ($filter->getBusinessHours() as $value) { if ($value == 'workingNow') { $boolFilter->addMust($workingNowFilter); } if ($value == '24/7') { $boolFilter->addMust($businessHoursIs24HFilter); } } } //Aggregation $aggregFilters = new \Elastica\Aggregation\Terms('filters'); $aggregFilters->setField('placeFilterValues.slug'); //$aggregFilters->setSize(0); $aggregCategories = new \Elastica\Aggregation\Terms('categories'); $aggregCategories->setField('categories.slug'); // $aggregBusinessHoursDay = new \Elastica\Aggregation\Filter('businessHoursDay'); // $aggregBusinessHoursDay->setFilter($businessHoursDayFilter); // // $aggregBusinessHoursStartsAt = new \Elastica\Aggregation\Filter('businessHoursStartsAt'); // $aggregBusinessHoursStartsAt->setFilter($businessHoursStartsAtFilter); // // $aggregBusinessHoursEndsAtFilter = new \Elastica\Aggregation\Filter('businessHoursEndsAt'); // $aggregBusinessHoursEndsAtFilter->setFilter($businessHoursEndsAtFilter); // // $aggregBusinessHoursStartsAt->addAggregation($aggregBusinessHoursEndsAtFilter); // $aggregBusinessHoursDay->addAggregation($aggregBusinessHoursStartsAt); $aggregBusinessHours = new \Elastica\Aggregation\Filters('businessHours'); $aggregBusinessHours->addFilter($workingNowFilter, 'workingNow'); $aggregBusinessHours->addFilter($businessHoursIs24HFilter, '24/7'); $filtered = new \Elastica\Query\Filtered($boolQuery, $boolFilter); $query = \Elastica\Query::create($filtered); //set aggregations type foreach ($filter->getAggregations() as $aggregation) { $aggtype = 'aggreg' . ucfirst($aggregation); $query->addAggregation(${$aggtype}); } //$query->addAggregation($aggregFilters); //$query->addAggregation($aggregBusinessHours); $sortMembership = array('membershipSubscriptions.membership.score' => array('nested_filter' => array('term' => array('membershipSubscriptions.m_status' => MembershipStatusType::ACTIVE)), 'order' => 'desc')); $sortRating = array('rating' => array('order' => 'desc')); $viewsCount = array('viewsCount' => array('order' => 'desc')); $sortTypes = array('membership' => array($sortMembership, $sortRating), 'rating' => array($sortRating), 'views' => array($viewsCount)); // if(!isset($sortTypes[$sort])){ // $sort = 'membership'; // } foreach ($sortTypes[$sort] as $s) { $query->addSort($s); } //$query->setFrom(4); //$query->addSort(array('rating' => array('order' => 'desc'))); //var_dump(json_encode($query->getQuery(), JSON_PRETTY_PRINT));die(); return $this->findPaginated($query); }
/** * @group unit */ public function testAddAggregationToArrayCast() { $query = new Query(); $aggregation = new \Elastica\Aggregation\Terms('text'); $aggregation->setField('field'); $query->addAggregation($aggregation); $aggregation->setName('another text'); $anotherQuery = new Query(); $anotherQuery->addAggregation($aggregation); $this->assertEquals($query->toArray(), $anotherQuery->toArray()); }