/**
  * @param ResponseInterface $response
  * @return SearchResultInterface
  */
 public function build(ResponseInterface $response)
 {
     /** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
     $searchResult = $this->searchResultFactory->create();
     $documents = iterator_to_array($response);
     $searchResult->setItems($documents);
     $searchResult->setAggregations($response->getAggregations());
     $searchResult->setTotalCount(count($documents));
     return $searchResult;
 }
Esempio n. 2
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     if ($this->filters) {
         foreach ($this->filters as $attributes) {
             foreach ($attributes as $attributeCode => $attributeValue) {
                 $attributeCode = $this->getAttributeCode($attributeCode);
                 $this->addAttributeToSearch($attributeCode, $attributeValue);
             }
         }
         $searchCriteria = $this->getSearchCriteriaBuilder()->create();
         $searchCriteria->setRequestName('advanced_search_container');
         try {
             $searchResult = $this->getSearch()->search($searchCriteria);
         } catch (EmptyRequestDataException $e) {
             /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
             $searchResult = $this->searchResultFactory->create()->setItems([]);
         } catch (NonExistingRequestNameException $e) {
             $this->_logger->error($e->getMessage());
             throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
         }
         $temporaryStorage = $this->temporaryStorageFactory->create();
         $table = $temporaryStorage->storeApiDocuments($searchResult->getItems());
         $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     }
     parent::_renderFiltersBefore();
 }
 /**
  * @param ResponseInterface $response
  * @return SearchResultInterface
  */
 public function build(ResponseInterface $response)
 {
     /** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
     $searchResult = $this->searchResultFactory->create();
     /** @var \Magento\Framework\Api\Search\DocumentInterface[] $documents */
     $documents = [];
     /** @var \Magento\Framework\Search\Document $responseDocument */
     foreach ($response as $responseDocument) {
         $document = $this->documentFactory->create();
         /** @var \Magento\Framework\Search\DocumentField $field */
         foreach ($responseDocument as $field) {
             $document->setCustomAttribute($field->getName(), $field->getValue());
         }
         $document->setId($responseDocument->getId());
         $documents[] = $document;
     }
     $searchResult->setItems($documents);
     $searchResult->setAggregations($response->getAggregations());
     $searchResult->setTotalCount(count($documents));
     return $searchResult;
 }
Esempio n. 4
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->getSearchCriteriaBuilder();
     $this->getFilterBuilder();
     $this->getSearch();
     if ($this->queryText) {
         $this->filterBuilder->setField('search_term');
         $this->filterBuilder->setValue($this->queryText);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->filterBuilder->setField('price_dynamic_algorithm');
         $this->filterBuilder->setValue($priceRangeCalculation);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $searchCriteria = $this->searchCriteriaBuilder->create();
     $searchCriteria->setRequestName($this->searchRequestName);
     try {
         $this->searchResult = $this->getSearch()->search($searchCriteria);
     } catch (EmptyRequestDataException $e) {
         /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
         $this->searchResult = $this->searchResultFactory->create()->setItems([]);
     } catch (NonExistingRequestNameException $e) {
         $this->_logger->error($e->getMessage());
         throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
     $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     $this->_totalRecords = $this->searchResult->getTotalCount();
     if ($this->order && 'relevance' === $this->order['field']) {
         $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
     }
     return parent::_renderFiltersBefore();
 }