/** * @return \Mirasvit\Search\Model\Index\AbstractIndex * @throws \Exception */ public function getIndexInstance() { if (!$this->indexInstance) { $code = $this->getData('code'); $this->indexInstance = $this->indexPool->get($code); if (!$this->indexInstance) { throw new \Exception(__("Instance for '%1' not found", $code)); } $this->indexInstance->setData($this->getData())->setModel($this); } return $this->indexInstance; }
/** * Constructor * * @param PageCollectionFactory $collectionFactory * @param CmsFilterProvider $cmsFilterProvider * @param IndexerFactory $indexer * @param SearcherFactory $searcher */ public function __construct(PageCollectionFactory $collectionFactory, CmsFilterProvider $cmsFilterProvider, EmailTemplateFactory $emailTemplateFactory, IndexerFactory $indexer, SearcherFactory $searcher) { $this->collectionFactory = $collectionFactory; $this->cmsFilterProvider = $cmsFilterProvider; $this->emailTemplateFactory = $emailTemplateFactory; parent::__construct($indexer, $searcher); }
/** * Rebuild store index * * @param int $storeId * @param null|array $ids * @return void */ public function rebuildStoreIndex($storeId, $ids = null) { if (!is_array($ids) && $ids != null) { $ids = [$ids]; } $pk = $this->index->getPrimaryKey(); $attributes = array_keys($this->index->getAttributeWeights()); $lastEntityId = 0; while (true) { $collection = $this->index->getSearchableEntities($storeId, $ids, $lastEntityId); if ($collection->count() == 0) { break; } /** @var DataObject $entity */ foreach ($collection as $entity) { $document = []; foreach ($attributes as $attribute) { $attributeId = $this->index->getAttributeId($attribute); $attributeValue = $entity->getData($attribute); $document[$attributeId] = $this->searchHelper->prepareDataIndex($attributeValue); } (yield $entity->getData($pk) => $document); $lastEntityId = $entity->getData($pk); } } }
/** * @param \Mirasvit\Search\Model\Index\AbstractIndex $index * @return array */ private function generateQuickSearchRequest($index) { $request = []; $request['dimensions']['scope']['name'] = 'scope'; $request['dimensions']['scope']['value'] = 'default'; $request['query'] = $index->getCode(); $request['index'] = $index->getCode(); $request['from'] = '0'; $request['size'] = '100'; $request['filters'] = []; $request['aggregations'] = []; $code = $index->getCode(); $request['queries'][$code]['type'] = 'boolQuery'; $request['queries'][$code]['name'] = $code; $request['queries'][$code]['boost'] = 1; $request['queries'][$code]['queryReference'] = [['clause' => 'should', 'ref' => 'search_query']]; $request['queries']['search_query']['name'] = $index->getCode(); $request['queries']['search_query']['type'] = 'matchQuery'; $request['queries']['search_query']['value'] = '$search_term$'; $request['queries']['search_query']['match'][] = ['field' => '*', 'boost' => 10]; return $request; }
/** * 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; }
/** * Save search weights for catalog attributes * {@inheritdoc} */ public function afterModelSave() { $attributes = $this->getModel()->getData('attributes'); if (!is_array($attributes)) { return parent::afterModelSave(); } $entityTypeId = $this->objectManager->create('Magento\\Eav\\Model\\Entity')->setType(Product::ENTITY)->getTypeId(); $collection = $this->attributeCollectionFactory->create()->addFieldToFilter('is_searchable', 1); /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ foreach ($collection as $attribute) { if (!in_array($attribute->getAttributeCode(), $attributes) && $attribute->getIsSearchable()) { $attribute->setIsSearchable(0)->save(); } } foreach ($attributes as $code => $weight) { /** @var \Magento\Eav\Model\Entity\Attribute $attribute */ $attribute = $this->eavAttribute->loadByCode($entityTypeId, $code); $attribute->setSearchWeight($weight)->setIsSearchable(1)->save(); } return parent::afterModelSave(); }
/** * Constructor * * @param StoreManagerInterface $storeManager * @param CategoryCollectionFactory $collectionFactory * @param IndexerFactory $indexer * @param SearcherFactory $searcher */ public function __construct(StoreManagerInterface $storeManager, CategoryCollectionFactory $collectionFactory, IndexerFactory $indexer, SearcherFactory $searcher) { $this->storeManager = $storeManager; $this->collectionFactory = $collectionFactory; parent::__construct($indexer, $searcher); }
/** * Constructor * * @param IndexerFactory $indexerFactory * @param SearcherFactory $searcherFactory * @param PostCollectionFactory $postCollectionFactory * @param ResourceConnection $resourceConnection */ public function __construct(IndexerFactory $indexerFactory, SearcherFactory $searcherFactory, PostCollectionFactory $postCollectionFactory, ResourceConnection $resourceConnection) { $this->postCollectionFactory = $postCollectionFactory; $this->resourceConnection = $resourceConnection; parent::__construct($indexerFactory, $searcherFactory); }