public function search(SiteSearch $siteSearch) { // We create a query to return all the articles but if the criteria text is specified, we use it if ($siteSearch->getText() != null && $siteSearch != '') { $baseQuery = new \Elastica\Query\MultiMatch(); $baseQuery->setQuery($siteSearch->getText())->setFields(array('title', 'subtitle', 'courseContent', 'content')); $baseQuery->setFuzziness(0.7); $baseQuery->setMinimumShouldMatch('80%'); } else { $baseQuery = new \Elastica\Query\MatchAll(); } // Then we create filters depending on the chosen criterias // Filter courses only if type is not "product" $productTypeFilter = new \Elastica\Filter\Type(); $productTypeFilter->setType('product'); $productNotFilter = new \Elastica\Filter\BoolNot($productTypeFilter); // Filter for products with available courses $nestedFilter = new \Elastica\Filter\Nested(); $nestedFilter->setPath('courses'); $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp())))); // Filter not(products) OR products with available courses $orFilter = new \Elastica\Filter\BoolOr(); $orFilter->addFilter($productNotFilter); $orFilter->addFilter($nestedFilter); // Create a bool filter to put everything together $boolFilter = new \Elastica\Filter\Bool(); $boolFilter->addMust($orFilter); // Filter type if ($siteSearch->getIsProduct() || $siteSearch->getIsInfoEvent() || $siteSearch->getIsContent()) { // Create OR filter to put together the types $typeOrFilter = new \Elastica\Filter\BoolOr(); // Filter products if ($siteSearch->getIsProduct()) { $productAndFilter = new \Elastica\Filter\BoolAnd(); $productAndFilter->addFilter($productTypeFilter); $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false)); $productAndFilter->addFilter($infoFilter); $typeOrFilter->addFilter($productAndFilter); } // Filter info events if isProduct is not selected if ($siteSearch->getIsInfoEvent()) { $productAndFilter = new \Elastica\Filter\BoolAnd(); $productAndFilter->addFilter($productTypeFilter); $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true)); $productAndFilter->addFilter($infoFilter); $typeOrFilter->addFilter($productAndFilter); } // Filter content if ($siteSearch->getIsContent()) { $typeOrFilter->addFilter($productNotFilter); } $boolFilter->addMust($typeOrFilter); } // Filter product type if ($siteSearch->getProductType()) { $productTypeFilter = new \Elastica\Filter\Nested(); $productTypeFilter->setPath('productType'); $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $siteSearch->getProductType()->getId()))); $boolFilter->addMust($productTypeFilter); } // Filter day time if ($siteSearch->getDayTime()) { $dayTimeFilter = new \Elastica\Filter\Nested(); $dayTimeFilter->setPath('courses'); $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $siteSearch->getDayTime()))); $boolFilter->addMust($dayTimeFilter); } // Filter category if ($siteSearch->getCategory()) { $categoryFilter = new \Elastica\Filter\BoolOr(); $mainCategoryFilter = new \Elastica\Filter\Nested(); $mainCategoryFilter->setPath('category'); $mainCategoryFilter->setFilter(new \Elastica\Filter\Term(array('category._id' => $siteSearch->getCategory()->getId()))); $subCategoryFilter = new \Elastica\Filter\Nested(); $subCategoryFilter->setPath('subcategory'); $subCategoryFilter->setFilter(new \Elastica\Filter\Term(array('subcategory._id' => $siteSearch->getCategory()->getId()))); $additionalCategoryFilter = new \Elastica\Filter\Nested(); $additionalCategoryFilter->setPath('additionalCategories'); $additionalCategoryFilter->setFilter(new \Elastica\Filter\Term(array('additionalCategories._id' => $siteSearch->getCategory()->getId()))); $categoryFilter->addFilter($mainCategoryFilter); $categoryFilter->addFilter($subCategoryFilter); $categoryFilter->addFilter($additionalCategoryFilter); $boolFilter->addMust($categoryFilter); } $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter); $query = \Elastica\Query::create($filtered); $sort = $siteSearch->getSort(); if (!empty($sort)) { $sort = explode(' ', $sort); $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc'))); } $paginated = $this->finder->findPaginated($query); $paginated->setMaxPerPage($siteSearch->getPerPage())->setCurrentPage($siteSearch->getPage()); return $paginated; }
public function search(Category $currentCategory, CategorySearch $categorySearch) { // We create a query to return all the products $baseQuery = new \Elastica\Query\MatchAll(); // Then we create filters depending on the chosen criterias // Filter products $productTypeFilter = new \Elastica\Filter\Type(); $productTypeFilter->setType('product'); // Filter for products with available courses $nestedFilter = new \Elastica\Filter\Nested(); $nestedFilter->setPath('courses'); $nestedFilter->setQuery(new \Elastica\Query\Range('beginDate', array('gte' => \Elastica\Util::convertDate((new \DateTime())->getTimestamp())))); // Create a bool filter to put everything together $boolFilter = new \Elastica\Filter\Bool(); $boolFilter->addMust($productTypeFilter); $boolFilter->addMust($nestedFilter); // Show only products // Filter type if ($categorySearch->getIsProduct() || $categorySearch->getIsInfoEvent()) { // Create OR filter to put together the types $typeOrFilter = new \Elastica\Filter\BoolOr(); // Filter products if ($categorySearch->getIsProduct()) { $productAndFilter = new \Elastica\Filter\BoolAnd(); $productAndFilter->addFilter($productTypeFilter); $infoFilter = new \Elastica\Filter\Term(array('infoVa' => false)); $productAndFilter->addFilter($infoFilter); $typeOrFilter->addFilter($productAndFilter); } // Filter info events if isProduct is not selected if ($categorySearch->getIsInfoEvent()) { $productAndFilter = new \Elastica\Filter\BoolAnd(); $productAndFilter->addFilter($productTypeFilter); $infoFilter = new \Elastica\Filter\Term(array('infoVa' => true)); $productAndFilter->addFilter($infoFilter); $typeOrFilter->addFilter($productAndFilter); } $boolFilter->addMust($typeOrFilter); } // Filter product type if ($categorySearch->getProductType()) { $productTypeFilter = new \Elastica\Filter\Nested(); $productTypeFilter->setPath('productType'); $productTypeFilter->setFilter(new \Elastica\Filter\Term(array('productType._id' => $categorySearch->getProductType()->getId()))); $boolFilter->addMust($productTypeFilter); } // Filter day time if ($categorySearch->getDayTime()) { $dayTimeFilter = new \Elastica\Filter\Nested(); $dayTimeFilter->setPath('courses'); $dayTimeFilter->setFilter(new \Elastica\Filter\Term(array('courses.dayTimes' => $categorySearch->getDayTime()))); $boolFilter->addMust($dayTimeFilter); } // Filter categories $categoryIds = array(); if ($categorySearch->getSubcategories() instanceof \Traversable) { foreach ($categorySearch->getSubcategories() as $category) { if (is_object($category)) { $categoryIds[] = $category->getId(); } else { $categoryIds[] = $category; } } } if (empty($categoryIds)) { $categoryIds[] = $currentCategory->getId(); foreach ($currentCategory->getChildren() as $child) { $categoryIds[] = $child->getId(); } } $categoryFilter = new \Elastica\Filter\BoolOr(); $mainCategoryFilter = new \Elastica\Filter\Nested(); $mainCategoryFilter->setPath('category'); $mainCategoryFilter->setFilter(new \Elastica\Filter\Terms('category._id', array($categoryIds))); $subCategoryFilter = new \Elastica\Filter\Nested(); $subCategoryFilter->setPath('subcategory'); $subCategoryFilter->setFilter(new \Elastica\Filter\Terms('subcategory._id', array($categoryIds))); $additionalCategoryFilter = new \Elastica\Filter\Nested(); $additionalCategoryFilter->setPath('additionalCategories'); $additionalCategoryFilter->setFilter(new \Elastica\Filter\Terms('additionalCategories._id', array($categoryIds))); $categoryFilter->addFilter($mainCategoryFilter); $categoryFilter->addFilter($subCategoryFilter); $categoryFilter->addFilter($additionalCategoryFilter); $boolFilter->addMust($categoryFilter); $filtered = new \Elastica\Query\Filtered($baseQuery, $boolFilter); $query = \Elastica\Query::create($filtered); $sort = $categorySearch->getSort(); if (!empty($sort)) { $sort = explode(' ', $sort); $query->setSort(array($sort[0] => array('order' => $sort[1]), "_score" => array('order' => 'desc'))); } $paginated = $this->finder->findPaginated($query); $paginated->setMaxPerPage($categorySearch->getPerPage())->setCurrentPage($categorySearch->getPage()); return $paginated; }
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); }
/** * @param string $field * @param Nested $nested * * @return \Elastica\Filter\Nested */ protected function getNestedFilter($field, $nested) { $filter = new \Elastica\Filter\Nested($field); $filter->setPath($field); $filter->setFilter($this->generateAndFilterBy($nested->getCriteria())); return $filter; }