public function testSearch() { $requestName = 'requestName'; $storeId = 333; $filterField = 'filterField'; $filterValue = 'filterValue'; $filter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->disableOriginalConstructor()->getMock(); $filter->expects($this->once())->method('getField')->willReturn($filterField); $filter->expects($this->once())->method('getValue')->willReturn($filterValue); $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->disableOriginalConstructor()->getMock(); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]); $searchCriteria = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchCriteriaInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $searchCriteria->expects($this->once())->method('getRequestName')->willReturn($requestName); $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $store->expects($this->once())->method('getId')->willReturn($storeId); $searchResult = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchResult')->disableOriginalConstructor()->getMockForAbstractClass(); $request = $this->getMockBuilder('Magento\\Framework\\Search\\RequestInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $response = $this->getMockBuilder('Magento\\Framework\\Search\\ResponseInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $this->requestBuilder->expects($this->once())->method('setRequestName')->with($requestName); $this->requestBuilder->expects($this->once())->method('bindDimension')->with('scope', $storeId); $this->requestBuilder->expects($this->any())->method('bind'); $this->requestBuilder->expects($this->once())->method('create')->willReturn($request); $this->searchEngine->expects($this->once())->method('search')->with($request)->willReturn($response); $this->searchResponseBuilder->expects($this->once())->method('build')->with($response)->willReturn($searchResult); $this->storeManager->expects($this->once())->method('getStore')->willReturn($store); $searchResult = $this->model->search($searchCriteria); $this->assertInstanceOf('Magento\\Framework\\Api\\Search\\SearchResultInterface', $searchResult); }
public function testSearch() { $request = $this->getMockBuilder('Magento\\Framework\\Search\\RequestInterface')->disableOriginalConstructor()->getMock(); $response = $this->getMockBuilder('Magento\\Framework\\Search\\ResponseInterface')->disableOriginalConstructor()->getMock(); $this->adapter->expects($this->once())->method('query')->with($this->equalTo($request))->will($this->returnValue($response)); $result = $this->searchEngine->search($request); $this->assertInstanceOf('Magento\\Framework\\Search\\ResponseInterface', $result); }
/** * @inheritdoc */ protected function _renderFiltersBefore() { $this->requestBuilder->bindDimension('scope', $this->getStoreId()); if ($this->queryText) { $this->requestBuilder->bind('search_term', $this->queryText); } $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($priceRangeCalculation) { $this->requestBuilder->bind('price_dynamic_algorithm', $priceRangeCalculation); } $this->requestBuilder->setRequestName('quick_search_container'); $queryRequest = $this->requestBuilder->create(); $this->queryResponse = $this->searchEngine->search($queryRequest); $ids = [0]; /** @var \Magento\Framework\Search\Document $document */ foreach ($this->queryResponse as $document) { $ids[] = $document->getId(); } parent::addFieldToFilter('entity_id', ['in' => $ids]); $this->_totalRecords = count($ids) - 1; if ($this->order && $this->order['field'] == 'relevance') { $this->getSelect()->order(new \Zend_Db_Expr($this->_conn->quoteInto('FIELD(e.entity_id, ?) ' . $this->order['dir'], $ids))); } return parent::_renderFiltersBefore(); }
/** * Load items count : * - collection size * * @return void */ private function loadItemCounts() { $storeId = $this->getStoreId(); $requestName = $this->searchRequestName; // Query text. $queryText = $this->queryText; $searchRequest = $this->requestBuilder->create($storeId, $requestName, 0, 0, $queryText, [], $this->filters, $this->queryFilters); $searchResponse = $this->searchEngine->search($searchRequest); $this->_totalRecords = $searchResponse->count(); }
/** * @inheritdoc */ protected function _renderFiltersBefore() { if ($this->filters) { $this->requestBuilder->bindDimension('scope', $this->getStoreId()); $this->requestBuilder->setRequestName('advanced_search_container'); foreach ($this->filters as $attributes) { foreach ($attributes as $attributeCode => $attributeValue) { $attributeCode = $this->getAttributeCode($attributeCode); $this->requestBuilder->bindRequestValue($attributeCode, $attributeValue); } } $queryRequest = $this->requestBuilder->create(); $queryResponse = $this->searchEngine->search($queryRequest); $ids = $this->getResponseIds($queryResponse); $this->addIdFilter($ids); } return parent::_renderFiltersBefore(); }
/** * @inheritdoc */ protected function _renderFiltersBefore() { if ($this->filters) { $this->requestBuilder->bindDimension('scope', $this->getStoreId()); $this->requestBuilder->setRequestName('advanced_search_container'); foreach ($this->filters as $attributes) { foreach ($attributes as $attributeCode => $attributeValue) { $attributeCode = $this->getAttributeCode($attributeCode); $this->requestBuilder->bindRequestValue($attributeCode, $attributeValue); } } $queryRequest = $this->requestBuilder->create(); $queryResponse = $this->searchEngine->search($queryRequest); $temporaryStorage = $this->temporaryStorageFactory->create(); $table = $temporaryStorage->storeDocuments($queryResponse->getIterator()); $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []); } return parent::_renderFiltersBefore(); }
/** * Load product count : * - collection size * - number of products by attribute set * * @return void */ private function loadProductCounts() { $storeId = $this->getStoreId(); $requestName = $this->searchRequestName; // Query text. $queryText = $this->queryText; $setIdFacet = ['attribute_set_id' => ['type' => BucketInterface::TYPE_TERM, 'config' => ['size' => 0]]]; $searchRequest = $this->requestBuilder->create($storeId, $requestName, 0, 0, $queryText, [], $this->filters, $this->queryFilters, $setIdFacet); $searchResponse = $this->searchEngine->search($searchRequest); $this->_totalRecords = $searchResponse->count(); $this->countByAttributeSet = []; $this->isSpellchecked = $searchRequest->isSpellchecked(); $bucket = $searchResponse->getAggregations()->getBucket('attribute_set_id'); if ($bucket) { foreach ($bucket->getValues() as $value) { $metrics = $value->getMetrics(); $this->countByAttributeSet[$metrics['value']] = $metrics['count']; } } }
/** * @inheritdoc */ protected function _renderFiltersBefore() { $this->requestBuilder->bindDimension('scope', $this->getStoreId()); if ($this->queryText) { $this->requestBuilder->bind('search_term', $this->queryText); } $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if ($priceRangeCalculation) { $this->requestBuilder->bind('price_dynamic_algorithm', $priceRangeCalculation); } $this->requestBuilder->setRequestName($this->searchRequestName); $queryRequest = $this->requestBuilder->create(); $this->queryResponse = $this->searchEngine->search($queryRequest); $temporaryStorage = $this->temporaryStorageFactory->create(); $table = $temporaryStorage->storeDocuments($this->queryResponse->getIterator()); $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []); $this->_totalRecords = $this->queryResponse->count(); if ($this->order && 'relevance' === $this->order['field']) { $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']); } return parent::_renderFiltersBefore(); }
/** * @inheritdoc * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _renderFiltersBefore() { if ($this->filters) { $this->requestBuilder->bindDimension('scope', $this->getStoreId()); $this->requestBuilder->setRequestName('advanced_search_container'); foreach ($this->filters as $attributes) { foreach ($attributes as $attributeCode => $attributeValue) { if (is_numeric($attributeCode)) { $attributeCode = $this->_eavConfig->getAttribute(Product::ENTITY, $attributeCode)->getAttributeCode(); } if (isset($attributeValue['from']) || isset($attributeValue['to'])) { if (isset($attributeValue['from']) && '' !== $attributeValue['from']) { $this->requestBuilder->bind("{$attributeCode}.from", $attributeValue['from']); } if (isset($attributeValue['to']) && '' !== $attributeValue['to']) { $this->requestBuilder->bind("{$attributeCode}.to", $attributeValue['to']); } } elseif (!is_array($attributeValue)) { $this->requestBuilder->bind($attributeCode, $attributeValue); } elseif (isset($attributeValue['like'])) { $this->requestBuilder->bind($attributeCode, trim($attributeValue['like'], '%')); } elseif (isset($attributeValue['in'])) { $this->requestBuilder->bind($attributeCode, $attributeValue['in']); } elseif (isset($attributeValue['in_set'])) { $this->requestBuilder->bind($attributeCode, $attributeValue['in_set']); } } } $queryRequest = $this->requestBuilder->create(); $queryResponse = $this->searchEngine->search($queryRequest); $ids = [0]; /** @var \Magento\Framework\Search\Document $document */ foreach ($queryResponse as $document) { $ids[] = $document->getId(); } $this->addIdFilter($ids); } return parent::_renderFiltersBefore(); }
/** * Join matches to collection * * @param AbstractDb $collection * @param string $field * * @return $this */ public function joinMatches($collection, $field = 'e.entity_id') { $requestBuilder = $this->requestBuilderFactory->create(); $queryText = $this->queryFactory->get()->getQueryText(); $requestBuilder->bind('search_term', $queryText); $requestBuilder->bindDimension('scope', $this->scopeResolver->getScope()); $requestBuilder->setRequestName($this->index->getCode()); $queryRequest = $requestBuilder->create(); $queryResponse = $this->searchEngine->search($queryRequest); $temporaryStorage = $this->temporaryStorageFactory->create(); if ($field == 'ID') { //external connection (need improve detection) $ids = [0]; foreach ($queryResponse->getIterator() as $item) { $ids[] = $item->getId(); } $collection->getSelect()->where(new \Zend_Db_Expr("{$field} IN (" . implode(',', $ids) . ")")); } else { $table = $temporaryStorage->storeDocuments($queryResponse->getIterator()); $collection->getSelect()->joinInner(['search_result' => $table->getName()], $field . ' = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []); } return $this; }