Esempio n. 1
0
 /**
  * Test a search using 'like' condition
  */
 public function testLike()
 {
     $attributeCode = 'description';
     $attributeCodeId = 42;
     $attribute = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false);
     $attribute->expects($this->once())->method('getAttributeCode')->willReturn($attributeCode);
     $this->eavConfig->expects($this->once())->method('getAttribute')->with(Product::ENTITY, $attributeCodeId)->willReturn($attribute);
     $filtersData = ['catalog_product_entity_text' => [$attributeCodeId => ['like' => 'search text']]];
     $this->filterBuilder->expects($this->once())->method('setField')->with($attributeCode)->willReturn($this->filterBuilder);
     $this->filterBuilder->expects($this->once())->method('setValue')->with('search text')->willReturn($this->filterBuilder);
     $filter = $this->getMock('Magento\\Framework\\Api\\Filter');
     $this->filterBuilder->expects($this->once())->method('create')->willReturn($filter);
     $criteria = $this->getMock('Magento\\Framework\\Api\\Search\\SearchCriteria', [], [], '', false);
     $this->criteriaBuilder->expects($this->once())->method('create')->willReturn($criteria);
     $criteria->expects($this->once())->method('setRequestName')->with('advanced_search_container');
     $tempTable = $this->getMock('Magento\\Framework\\DB\\Ddl\\Table', [], [], '', false);
     $temporaryStorage = $this->getMock('Magento\\Framework\\Search\\Adapter\\Mysql\\TemporaryStorage', [], [], '', false);
     $temporaryStorage->expects($this->once())->method('storeApiDocuments')->willReturn($tempTable);
     $this->temporaryStorageFactory->expects($this->once())->method('create')->willReturn($temporaryStorage);
     $searchResult = $this->getMock('Magento\\Framework\\Api\\Search\\SearchResultInterface', [], [], '', false);
     $this->search->expects($this->once())->method('search')->willReturn($searchResult);
     // addFieldsToFilter will load filters,
     //   then loadWithFilter will trigger _renderFiltersBefore code in Advanced/Collection
     $this->assertSame($this->advancedCollection, $this->advancedCollection->addFieldsToFilter($filtersData)->loadWithFilter());
 }
Esempio n. 2
0
 /**
  * @param QueryContainerFactory   $queryContainerFactory
  * @param Match                   $matchBuilder
  * @param TemporaryStorageFactory $temporaryStorageFactory
  * @param IndexFactory            $indexFactory
  * @param IndexScopeResolver      $scopeResolver
  * @param Engine                  $engine
  * @param MysqlDocumentFactory    $documentFactory
  */
 public function __construct(QueryContainerFactory $queryContainerFactory, Match $matchBuilder, TemporaryStorageFactory $temporaryStorageFactory, IndexFactory $indexFactory, IndexScopeResolver $scopeResolver, Engine $engine, MysqlDocumentFactory $documentFactory)
 {
     $this->queryContainerFactory = $queryContainerFactory;
     $this->matchBuilder = $matchBuilder;
     $this->temporaryStorage = $temporaryStorageFactory->create();
     $this->indexFactory = $indexFactory;
     $this->scopeResolver = $scopeResolver;
     $this->engine = $engine;
     $this->documentFactory = $documentFactory;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function query(RequestInterface $request)
 {
     $query = $this->mapper->buildQuery($request);
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocumentsFromSelect($query);
     $documents = $this->getDocuments($table);
     $aggregations = $this->aggregationBuilder->build($request, $table, $documents);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
Esempio n. 4
0
 /**
  * @param ScoreBuilderFactory $scoreBuilderFactory
  * @param Builder $filterBuilder
  * @param ConditionManager $conditionManager
  * @param ResourceConnection $resource
  * @param EntityMetadata $entityMetadata
  * @param QueryContainerFactory $queryContainerFactory
  * @param Query\Builder\Match $matchBuilder
  * @param TemporaryStorageFactory $temporaryStorageFactory
  * @param IndexBuilderInterface[] $indexProviders
  */
 public function __construct(ScoreBuilderFactory $scoreBuilderFactory, Builder $filterBuilder, ConditionManager $conditionManager, ResourceConnection $resource, EntityMetadata $entityMetadata, QueryContainerFactory $queryContainerFactory, Match $matchBuilder, TemporaryStorageFactory $temporaryStorageFactory, array $indexProviders)
 {
     $this->scoreBuilderFactory = $scoreBuilderFactory;
     $this->filterBuilder = $filterBuilder;
     $this->conditionManager = $conditionManager;
     $this->resource = $resource;
     $this->entityMetadata = $entityMetadata;
     $this->indexProviders = $indexProviders;
     $this->queryContainerFactory = $queryContainerFactory;
     $this->matchBuilder = $matchBuilder;
     $this->temporaryStorage = $temporaryStorageFactory->create();
 }
Esempio n. 5
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();
 }
 /**
  * @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);
     $this->searchResult = $this->search->search($searchCriteria);
     $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();
 }
 /**
  * {@inheritdoc}
  */
 public function query(RequestInterface $request)
 {
     $query = $this->catalogSearchHelper->getEscapedQueryText();
     $storeId = $this->storeManager->getStore()->getId();
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $documents = [];
     $table = null;
     if (!$this->config->getApplicationID($storeId) || !$this->config->getAPIKey($storeId) || $this->config->isEnabledFrontEnd($storeId) === false || $this->config->makeSeoRequest($storeId) === '0' || $this->request->getControllerName() === 'category' && $this->config->replaceCategories($storeId) == false) {
         $query = $this->mapper->buildQuery($request);
         $table = $temporaryStorage->storeDocumentsFromSelect($query);
         $documents = $this->getDocuments($table);
     } else {
         $algolia_query = $query !== '__empty__' ? $query : '';
         //If instant search is on, do not make a search query unless SEO request is set to 'Yes'
         if (!$this->config->isInstantEnabled($storeId) || $this->config->makeSeoRequest($storeId)) {
             $documents = $this->algoliaHelper->getSearchResult($algolia_query, $storeId);
         }
         $getDocumentMethod = 'getDocument21';
         $storeDocumentsMethod = 'storeApiDocuments';
         if (version_compare($this->config->getMagentoVersion(), '2.1.0', '<') === true) {
             $getDocumentMethod = 'getDocument20';
             $storeDocumentsMethod = 'storeDocuments';
         }
         $apiDocuments = array_map(function ($document) use($getDocumentMethod) {
             return $this->{$getDocumentMethod}($document);
         }, $documents);
         $table = $temporaryStorage->{$storeDocumentsMethod}($apiDocuments);
     }
     $aggregations = $this->aggregationBuilder->build($request, $table);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function query(RequestInterface $request)
 {
     $query = $this->mapper->buildQuery($request);
     if ($request->getName() == 'quick_search_container') {
         $query->limit($this->searchConfig->getResultsLimit());
     }
     if (isset($_GET) && isset($_GET['debug'])) {
         echo '<hr>' . $query . '<hr>';
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocumentsFromSelect($query);
     $this->searchHelper->prepareTemporaryTable($table);
     $documents = $this->getDocuments($table);
     $aggregations = $this->aggregationBuilder->build($request, $table);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
Esempio n. 9
0
 /**
  * @param RequestInterface $request
  * @return \Magento\Framework\Search\Response\QueryResponse
  */
 public function query(RequestInterface $request)
 {
     try {
         $query = $this->mapper->buildQuery($request);
         $query->limit($this->searchConfig->getResultsLimit());
     } catch (\Exception $e) {
         // fallback engine
         $objectManager = ObjectManager::getInstance();
         return $objectManager->create('Mirasvit\\SearchMysql\\Model\\Adapter')->query($request);
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeDocumentsFromSelect($query);
     $this->searchHelper->prepareTemporaryTable($table);
     $documents = $this->getDocuments($table);
     $aggregations = $this->aggregationBuilder->build($request, $table);
     $response = ['documents' => $documents, 'aggregations' => $aggregations];
     return $this->responseFactory->create($response);
 }
 /**
  * @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->searchCriteriaBuilder->create();
         $searchCriteria->setRequestName('advanced_search_container');
         $searchResult = $this->search->search($searchCriteria);
         $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();
 }
Esempio n. 11
0
 /**
  * @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();
 }
Esempio n. 12
0
 /**
  * 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;
 }
Esempio n. 13
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();
 }