/** * Append the user fulltext query to the tracked variables list * * @return array */ public function getVariables() { $variables = ['search.query' => $this->catalogSearchData->getEscapedQueryText()]; if ($layer = $this->catalogLayer) { $productCollection = $layer->getProductCollection(); $variables['search.is_spellchecked'] = (int) $productCollection->isSpellchecked(); } return $variables; }
/** * @param bool $isMinQueryLength * @param string $expectedResult * @dataProvider getNoResultTextDataProvider */ public function testGetNoResultText($isMinQueryLength, $expectedResult) { $this->dataMock->expects($this->once())->method('isMinQueryLength')->will($this->returnValue($isMinQueryLength)); if ($isMinQueryLength) { $queryMock = $this->getMock('Magento\\Search\\Model\\Query', [], [], '', false); $queryMock->expects($this->once())->method('getMinQueryLength')->will($this->returnValue('5')); $this->queryFactoryMock->expects($this->once())->method('get')->will($this->returnValue($queryMock)); } $this->assertEquals($expectedResult, $this->model->getNoResultText()); }
/** * Append the fuzziness alert message + save the search result count. * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @param \Magento\CatalogSearch\Block\Result $resultBlock Result block. * @param \Closure $proceed Original method. * * @return string[] */ public function aroundGetNoteMessages(Result $resultBlock, \Closure $proceed) { $messages = $proceed(); $query = $this->queryFactory->get(); $query->setNumResults($this->resultCount); $query->setIsSpellchecked(false); if ($this->isSpellcheck() && $this->resultCount > 0) { $messages[] = __("No search results for: <b>'%1'</b>. We propose you approaching results.", $this->catalogSearchData->getEscapedQueryText()); $query->setIsSpellchecked(true); } $this->queryResource->saveSearchResults($query); return $messages; }
/** * Load search results * * @return $this */ public function load() { $result = array(); if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) { $this->setResults($result); return $this; } $collection = $this->_catalogSearchData->getQuery()->getSearchCollection()->addAttributeToSelect('name')->addAttributeToSelect('description')->addBackendSearchFilter($this->getQuery())->setCurPage($this->getStart())->setPageSize($this->getLimit())->load(); foreach ($collection as $product) { $description = strip_tags($product->getDescription()); $result[] = array('id' => 'product/1/' . $product->getId(), 'type' => __('Product'), 'name' => $product->getName(), 'description' => $this->string->substr($description, 0, 30), 'url' => $this->_adminhtmlData->getUrl('catalog/product/edit', array('id' => $product->getId()))); } $this->setResults($result); return $this; }
/** * {@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); }
/** * Prepare results for query * * @param Query $query * @return $this */ public function prepareResult($query = null) { if (!$query instanceof Query) { $query = $this->_catalogSearchData->getQuery(); } $queryText = $this->_catalogSearchData->getQueryText(); if ($query->getSynonymFor()) { $queryText = $query->getSynonymFor(); } $this->getResource()->prepareResult($this, $queryText, $query); return $this; }
/** * Prepare Fulltext index value for product * * @param array $indexData * @param array $productData * @param int $storeId * @return string */ protected function prepareProductIndex($indexData, $productData, $storeId) { $index = []; foreach ($this->getSearchableAttributes('static') as $attribute) { $attributeCode = $attribute->getAttributeCode(); if (isset($productData[$attributeCode])) { $value = $this->getAttributeValue($attribute->getId(), $productData[$attributeCode], $storeId); if ($value) { if (isset($index[$attributeCode])) { if (!is_array($index[$attributeCode])) { $index[$attributeCode] = [$index[$attributeCode]]; } $index[$attributeCode][] = $value; } else { $index[$attributeCode] = $value; } } } } foreach ($indexData as $entityId => $attributeData) { foreach ($attributeData as $attributeId => $attributeValue) { $value = $this->getAttributeValue($attributeId, $attributeValue, $storeId); if (!is_null($value) && $value !== false) { $attributeCode = $this->getSearchableAttribute($attributeId)->getAttributeCode(); if (isset($index[$attributeCode])) { $index[$attributeCode][$entityId] = $value; } else { $index[$attributeCode] = [$entityId => $value]; } } } } if (!$this->engineProvider->get()->allowAdvancedIndex()) { $product = $this->getProductEmulator($productData['type_id'])->setId($productData['entity_id'])->setStoreId($storeId); $typeInstance = $this->getProductTypeInstance($productData['type_id']); $data = $typeInstance->getSearchableData($product); if ($data) { $index['options'] = $data; } } if ($this->engineProvider->get()) { return $this->engineProvider->get()->prepareEntityIndex($index, $this->separator); } return $this->catalogSearchData->prepareIndexdata($index, $this->separator); }
public function testGetMaxQueryLength() { $return = 'some_value'; $this->_scopeConfigMock->expects($this->once())->method('getValue')->with(\Magento\Search\Model\Query::XML_PATH_MAX_QUERY_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)->will($this->returnValue($return)); $this->assertEquals($return, $this->_model->getMaxQueryLength()); }
public function testCheckNotesResult() { $this->assertInstanceOf('Magento\\CatalogSearch\\Helper\\Data', $this->_helper->checkNotes()); }
/** * @param \Magento\Catalog\Model\Resource\Product\Collection $collection * @param \Magento\Catalog\Model\Category $category * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function filter($collection, \Magento\Catalog\Model\Category $category) { $collection->addAttributeToSelect($this->catalogConfig->getProductAttributes())->addSearchFilter($this->helper->getQuery()->getQueryText())->setStore($this->storeManager->getStore())->addMinimalPrice()->addFinalPrice()->addTaxPercents()->addStoreFilter()->addUrlRewrite()->setVisibility($this->productVisibility->getVisibleInSearchIds()); }
/** * Prepare index array as a string glued by separator * * @param array $index * @param string $separator * @return string */ public function prepareEntityIndex($index, $separator = ' ') { return $this->_catalogSearchData->prepareIndexdata($index, $separator); }
/** * Retrieve query model object * * @return \Magento\CatalogSearch\Model\Query */ protected function _getQuery() { return $this->_catalogSearchData->getQuery(); }
/** * Retrieve Note messages * * @return array */ public function getNoteMessages() { return $this->catalogSearchData->getNoteMessages(); }
/** * @param \Magento\Catalog\Model\Category $category * @return string|void */ public function toString($category) { return 'Q_' . $this->helper->getQuery()->getId() . '_' . parent::toString($category); }
/** * Append message to the customer session to inform he has been redirected * * @param \Magento\Catalog\Api\Data\ProductInterface $product The product being redirected to. */ private function addRedirectMessage(ProductInterface $product) { $message = __("%1 is the only product matching your '%2' research.", $product->getName(), $this->helper->getEscapedQueryText()); $this->messageManager->addSuccessMessage($message); }