/** * @param $itemsPerPage * @param $currentPage * @param $sort * @param $currency * @param $country * @param array $filters * @param array $facets * @return array */ public function getProducts($itemsPerPage, $currentPage, $sort, $currency, $country, $filters = null, $facets = null) { $searchRequest = ProductProjectionSearchRequest::of()->sort($sort)->limit($itemsPerPage)->currency($currency)->country($country)->offset(min($itemsPerPage * ($currentPage - 1), 100000)); if (!is_null($facets)) { foreach ($facets as $facet) { $searchRequest->addFacet($facet); } } if (!is_null($filters)) { foreach ($filters as $type => $typeFilters) { foreach ($typeFilters as $filter) { switch ($type) { case 'filter': $searchRequest->addFilter($filter); break; case 'filter.query': $searchRequest->addFilterQuery($filter); break; case 'filter.facets': $searchRequest->addFilterFacets($filter); break; } } } } $this->profiler->enter($profile = new Profile('getProducts')); $response = $searchRequest->executeWithClient($this->client); $this->profiler->leave($profile); $products = $searchRequest->mapResponse($response); return [$products, $response->getFacets(), $response->getOffset(), $response->getTotal()]; }
public function addFacets(ProductProjectionSearchRequest $request, $selectedValues = [], array $showFacets = null) { if (is_null($showFacets)) { $showFacets = array_keys($this->facetConfigs); } foreach ($showFacets as $facetName) { $facetConfig = $this->facetConfigs[$facetName]; $facet = Facet::ofName($facetConfig->getFacetField())->setAlias($facetConfig->getAlias()); if ($facetConfig->getType() == FacetConfig::TYPE_RANGE) { $facet->setValue($facetConfig->getRanges()); } $request->addFacet($facet); if (isset($selectedValues[$facetName])) { $filter = Filter::ofName($facetConfig->getFilterField()); if ($facetConfig->getType() == FacetConfig::TYPE_RANGE) { $filter->setValue($facetConfig->createRanges($selectedValues[$facetName])); } elseif (!$facetConfig->isHierarchical()) { $filter->setValue($selectedValues[$facetName]); } else { $subtree = FilterSubtreeCollection::of(); if (!is_array($selectedValues[$facetName])) { $selectedValues[$facetName] = [$selectedValues[$facetName]]; } foreach ($selectedValues[$facetName] as $value) { $subtree->add(FilterSubtree::ofId($value)); } $filter->setValue($subtree); } if ($facetConfig->isMultiSelect()) { $request->addFilter($filter); $request->addFilterFacets($filter); } else { $request->addFilterQuery($filter); } } } return $request; }
/** * @param $locale * @param $itemsPerPage * @param $currentPage * @param $sort * @param $currency * @param $country * @param $search * @param UriInterface $uri * @param array $filters * @return array */ public function getProducts($locale, $itemsPerPage, $currentPage, $sort, $currency, $country, $search = null, UriInterface $uri, $filters = null) { $searchRequest = ProductProjectionSearchRequest::of()->sort($sort)->limit($itemsPerPage)->currency($currency)->country($country)->offset(min($itemsPerPage * ($currentPage - 1), 100000)); if (!is_null($search)) { $language = \Locale::getPrimaryLanguage($locale); $searchRequest->addParam('text.' . $language, $search); $searchRequest->fuzzy(true); } $selectedValues = $this->searchModel->getSelectedValues($uri); $searchRequest = $this->searchModel->addFacets($searchRequest, $selectedValues); if (!is_null($filters)) { foreach ($filters as $type => $typeFilters) { foreach ($typeFilters as $filter) { switch ($type) { case 'filter': $searchRequest->addFilter($filter); break; case 'filter.query': $searchRequest->addFilterQuery($filter); break; case 'filter.facets': $searchRequest->addFilterFacets($filter); break; } } } } $response = $searchRequest->executeWithClient($this->getClient()); $products = $searchRequest->mapFromResponse($response, $this->getMapper($locale)); return [$products, $response->getFacets(), $response->getOffset(), $response->getTotal()]; }