private function makeElasticQuery() { $exceptedQuery = new \Elastica\Query(); $aggregationFilters = new \Elastica\Aggregation\Filters("emotion"); $negative = new \Elastica\Filter\Range('valeur', array('lt' => 0)); $aggregationFilters->addFilter($negative, 'negative'); $neutral = new \Elastica\Filter\Range('valeur', array('lt' => 1, 'gt' => -1)); $aggregationFilters->addFilter($neutral, 'neutral'); $positive = new \Elastica\Filter\Range('valeur', array('gt' => 0)); $aggregationFilters->addFilter($positive, 'positive'); $dateHisto = new \Elastica\Aggregation\DateHistogram('nb', 'date', 'month'); $aggregationFilters->addAggregation($dateHisto); $exceptedQuery->addAggregation($aggregationFilters); return $exceptedQuery; }
public function getColorAttributes($allArticles, $elasticIndexVariants, $colors) { foreach ($allArticles as $article) { $article_ids[] = $article->getId(); } $boolQuery = new \Elastica\Query\BoolQuery(); $articleRefQuery = new \Elastica\Query\Terms(); $articleRefQuery->setTerms('articleRef', $article_ids); $boolQuery->addMust($articleRefQuery); $query = new \Elastica\Query(); $query->setQuery($boolQuery); $agg2 = new \Elastica\Aggregation\Terms("variantsattr"); $agg2->setField('variantvalues.otherTerms'); $agg2->setSize(50); $filter = new \Elastica\Filter\Term(); $filter->setTerm('attributeRef', 1); $agg4 = new \Elastica\Aggregation\Filters('filterColor'); $agg4->addFilter($filter); $agg4->addAggregation($agg2); $cAgg = new \Elastica\Aggregation\Nested('colors', 'variantvalues'); $cAgg->addAggregation($agg4); $query->addAggregation($cAgg); $aggResult = $elasticIndexVariants->search($query); $colors = $aggResult->getAggregations(); $result = []; foreach ($colors['colors']['filterColor']['buckets'][0]['variantsattr']['buckets'] as $color) { $result[] = $color; } return $result; }
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); }