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

搜索

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

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

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

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

    <type name="Magento\Search\Model\Adminhtml\System\Config\Source\Engine">
        <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="Magento\CatalogSearch\Model\ResourceModel\EngineProvider">
        <arguments>
            <argument name="engines" xsi:type="array">
                <item name="solr" xsi:type="string">Inchoo\Solr\Model\ResourceModel\Engine</item>
            </argument>
        </arguments>
    </type> 

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

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

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

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

<?php
namespace Inchoo\Solr\Model\ResourceModel;
 
use Magento\CatalogSearch\Model\ResourceModel\EngineInterface;
 
 
class Engine implements EngineInterface
{
 
    protected $catalogProductVisibility;
    private $indexScopeResolver;
 
    public function __construct(
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver $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="Magento\CatalogSearch\Model\Indexer\IndexerHandlerFactory">
        <arguments>
            <argument name="handlers" xsi:type="array">
                <item name="solr" xsi:type="string">Inchoo\Solr\Model\Indexer\IndexerHandler</item>
            </argument>
        </arguments>
    </type>

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

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

<?php
namespace Inchoo\Solr\Model\Indexer;
 
use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
use Magento\Framework\Indexer\IndexStructureInterface;
use Magento\Framework\Search\Request\Dimension;
use Magento\Framework\Search\Request\IndexScopeResolverInterface;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
 
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="Magento\Search\Model\AdapterFactory">
        <arguments>
            <argument name="adapters" xsi:type="array">
                <item name="solr" xsi:type="string">Inchoo\Solr\SearchAdapter\Adapter</item>
            </argument>
        </arguments>
    </type>

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

<?php
namespace Inchoo\Solr\SearchAdapter;
 
use Magento\Framework\Search\AdapterInterface;
use Magento\Framework\Search\RequestInterface;
use Magento\Framework\Search\Response\QueryResponse;
use Inchoo\Solr\SearchAdapter\Aggregation\Builder;
 
 
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。

posted @ 2016-10-14 15:10  magento  阅读(1022)  评论(0编辑  收藏  举报

Magento推荐:Magento | Magento | Magento开发 | Magento官网

Magento学习博客:Magento

友情链接:FacebookMessenger