• 麦进斗Magento2添加新的产品搜索引擎


    搜索

    Magento的2社区版配备了支持,仅在MySQL的搜索引擎,但有些项目需要,以增加销售或转化率更好或更可调整的搜索引擎。
    在这种情况下,我们正在实施的Solr或Elasticsearch搜索引擎。

    在这篇文章中,我们将创建一个框架代码或粗糙的例子介绍,使我们能够实现像Solr的或额外的Elasticsearch搜索引擎主要的类和方法。如果你把在Magento 2管理员一起来看看,你可以找到一个位置的搜索引擎配置:商店- >配置- >目录- >目录搜索和下拉“搜索引擎”

    在下拉列表中你会发现,你只有MySQL的引擎和我们的第一步将是在这个下拉列表中添加产生额外选项带有标签的“Solr”。所以,让我们开始。

    按照往常一样,你需要创建一个Magento的2模块(我想你已经知道了这个过程,但如果你不这样做,你可以阅读教程在这里)。在etc文件夹你的模块,你需要创建下一个XML代码文件di.xml:

        <type name="MagentoSearchModelAdminhtmlSystemConfigSourceEngine">
            <arguments>
                <argument name="engines" xsi:type="array">
                    <item name="solr" xsi:type="string">Solr</item>
                </argument>
            </arguments>
        </type>

    有了这个XML代码我们增加了一个新的选项与选项名称为“我们的下拉列表中的Solr ”。如果创建得当和清理Magento的缓存,你就可以看到它在下拉那里将是一个新的选项“Solr的”。如果你看到它那么就意味着你添加正确。

    在下一步中,我们将与PHP类是负责索引数据进行搜索服务器启动。

    首先,我们应该执行引擎类,放在di.xml下面的代码:

        <type name="MagentoCatalogSearchModelResourceModelEngineProvider">
            <arguments>
                <argument name="engines" xsi:type="array">
                    <item name="solr" xsi:type="string">InchooSolrModelResourceModelEngine</item>
                </argument>
            </arguments>
        </type> 

    你可以看到,我们推出了我们自己的引擎类“ Inchoo Solr的型号 ResourceModel 引擎 ”。引擎类负责准备数据它(Solr的服务器之前,最后一个端点)去我们indexerHandler课前和引擎类必须实现: Magento的 CatalogSearch 型号 ResourceModel EngineInterface

    接口类包含接下来的四个方法:
    processAttributeValue准备属性值在Solr的索引存储
    getAllowedVisibility检索当前发动机允许值的可见性
    allowAdvancedIndex定义,如果目前的搜索引擎支持先进的指标
    prepareEntityIndex准备数组索引由分隔粘字符串

    这些方法是强制性的,在你的引擎类来实现。为了更好地理解,您可以检查/类似于MySQL的本机类的比较逻辑:Magento的 CatalogSearch 型号 ResourceModel 发动机

    我们的骨架类的例子如下:

    <?php
    namespace InchooSolrModelResourceModel;
     
    use MagentoCatalogSearchModelResourceModelEngineInterface;
     
     
    class Engine implements EngineInterface
    {
     
        protected $catalogProductVisibility;
        private $indexScopeResolver;
     
        public function __construct(
            MagentoCatalogModelProductVisibility $catalogProductVisibility,
            MagentoFrameworkIndexerScopeResolverIndexScopeResolver $indexScopeResolver
        ) {
            $this->catalogProductVisibility = $catalogProductVisibility;
            $this->indexScopeResolver = $indexScopeResolver;
        }
     
        public function getAllowedVisibility()
        {
            return $this->catalogProductVisibility->getVisibleInSiteIds();
        }
     
        public function allowAdvancedIndex()
        {
            return false;
        }
     
        public function processAttributeValue($attribute, $value)
        {
            return $value;
        }
     
        public function prepareEntityIndex($index, $separator = ' ')
        {
            return $index;
        }
     
        public function isAvailable()
        {
            return true;
        }
    }

    下一步是名称为“创建indexerHandler Inchoo Solr的型号索引器 IndexerHandler有落实” 的Magento 框架索引器 SaveHandler IndexerInterface
    对于IndexerHandler的实现,你应该在你的di.xml文件中添加下面的代码:

       <type name="MagentoCatalogSearchModelIndexerIndexerHandlerFactory">
            <arguments>
                <argument name="handlers" xsi:type="array">
                    <item name="solr" xsi:type="string">InchooSolrModelIndexerIndexerHandler</item>
                </argument>
            </arguments>
        </type>

    如果打开IndexerInterface你会看到四种方法,你必须实现:
    saveIndex实体数据添加到索引
    deleteIndex从索引中删除实体的数据
    cleanIndex从索引中删除所有数据
    isAvailable定义是否引擎可用(可以实现平给Solr服务器和检查直播)。

    我们IndexerHandler骨架类的例子如下:

    <?php
    namespace InchooSolrModelIndexer;
     
    use MagentoEavModelConfig;
    use MagentoFrameworkAppResourceConnection;
    use MagentoFrameworkDBAdapterAdapterInterface;
    use MagentoFrameworkIndexerSaveHandlerIndexerInterface;
    use MagentoFrameworkIndexerIndexStructureInterface;
    use MagentoFrameworkSearchRequestDimension;
    use MagentoFrameworkSearchRequestIndexScopeResolverInterface;
    use MagentoFrameworkIndexerSaveHandlerBatch;
    use MagentoFrameworkIndexerScopeResolverIndexScopeResolver;
     
    class IndexerHandler implements IndexerInterface
    {
        private $indexStructure;
     
        private $data;
     
        private $fields;
     
        private $resource;
     
        private $batch;
     
        private $eavConfig;
     
        private $batchSize;
     
        private $indexScopeResolver;
     
        public function __construct(
            Batch $batch,
            array $data,
            $batchSize = 50
        ) {
            $this->batch = $batch;
            $this->data = $data;
            $this->batchSize = $batchSize;
        }
     
        public function saveIndex($dimensions, Traversable $documents)
        {
            foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
     
            }
        }
     
        public function deleteIndex($dimensions, Traversable $documents)
        {
            foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
     
            }
        }
     
        public function cleanIndex($dimensions)
        {
     
        }
     
        public function isAvailable()
        {
            return true;
        }
    }

    在这些方法中,你应该实现Solr的PHP客户端将进入上市操作的Solr服务器。很多时候,采用的是日光浴PHP客户端

    有了这一步,我们与索引数据的过程中进行搜索服务器结束。

    现在,你可以检查是你索引可以与下一个命令(前集搜索引擎SOLR在Magento管理):

    php /bin/magento indexer:reindex catalogsearch_fulltext

    在接下来的,最后一步,我们将解释如何实现对Magento的前端2新的搜索引擎。同时,我们必须修改di.xml并添加下面的代码:

    <type name="MagentoSearchModelAdapterFactory">
            <arguments>
                <argument name="adapters" xsi:type="array">
                    <item name="solr" xsi:type="string">InchooSolrSearchAdapterAdapter</item>
                </argument>
            </arguments>
        </type>

    我们新的适配器类Inchoo Solr的 SearchAdapter 适配器适配器类应该实现  的Magento 框架搜索 AdapterInterface在我们的适配器,我们必须实现的方法查询 -这个方法接受查询请求和处理。看看我们的例子中,一切都将更加清晰。

    <?php
    namespace InchooSolrSearchAdapter;
     
    use MagentoFrameworkSearchAdapterInterface;
    use MagentoFrameworkSearchRequestInterface;
    use MagentoFrameworkSearchResponseQueryResponse;
    use InchooSolrSearchAdapterAggregationBuilder;
     
     
    class Adapter implements AdapterInterface
    {
        protected $responseFactory;
     
        protected $connectionManager;
     
        protected $aggregationBuilder;
     
        public function __construct(
            ResponseFactory $responseFactory,
            Builder $aggregationBuilder,
            ConnectionManager $connectionManager
        ) {
            $this->responseFactory = $responseFactory;
            $this->aggregationBuilder = $aggregationBuilder;
            $this->connectionManager = $connectionManager;
     
        }
     
        /**
         * @param RequestInterface $request
         * @return QueryResponse
         */
        public function query(RequestInterface $request)
        {
            $client = $this->getConnection();
            $documents = [];
     
            $documents[1007] = array('entity_id'=>'1007', 'score'=>46.055);
            $documents[1031] = array('entity_id'=>'1031', 'score'=>45.055);
            $documents[1120] = array('entity_id'=>'1120', 'score'=>44.055);
     
            $aggregations = $this->aggregationBuilder->build($request, $documents);
     
            $response = [
                'documents' => $documents,
                'aggregations' => $aggregations,
            ];
            return $this->responseFactory->create($response);
        }
     
        public function getConnection(){
            return $this->connectionManager->getConnection();
        }
    }

    在我们的演示适配器类,我们硬编码产物entity_ids:1007 1031,1120年从我们的数据库产品标识,仅用于测试目的。如果你想更深入,我建议你检查逻辑MySQL的本地适配器如何工作的。

    有了这个步骤中,我们结束我们的榜样。尽管事情似乎相当复杂,当你开始工作,一切都会好起来。我希望你会喜欢你的新的搜索引擎的编码磁2。

  • 相关阅读:
    oracle中 sql%rowcount 使用方法
    【玩转微信公众平台之九】 第一个功能:消息自己主动回复
    silverlight中datagrid数据到处excel
    列表
    Java实现网格中移动字母
    Java实现有理数的循环节
    Java实现有理数的循环节
    Java实现有理数的循环节
    Java实现有理数的循环节
    Java实现有理数的循环节
  • 原文地址:https://www.cnblogs.com/magento-maijindou/p/5960554.html
Copyright © 2020-2023  润新知