eZ Platform Discussions

Upgrade from 5.4. Document Field Mappers


#1

Upgrade from 5.4. Document Field Mappers
[Solr] Upgrade from 5.4. transform ExtendedEzfSolrDocumentFieldBase to Document Field Mappers.

Bonjour,

I am on a migration from eZ Publish 5.4 to eZPLarform 1.13.

In my old site I had developed an implementation of ezfSolrDocumentFieldBase to index in solr specific business information to my products.

As I knew that I was going to have to do this migration, I decided to rely on the new stack as soon as possible in order to reduce the ‘disposable’ code as much as possible.

Here is my implementation of my ezfSolrDocumentFieldBase and its configuration.

src/MyClient/LegacyBundle/ezpublish_legacy/myclient/classes/ProductIndexing.php

/**
 * The ProductIndexing.php class handles indexing and products in eZFind.
 * @see src/MyClient/FrontEndBundle/Helper/ProductHelper.php
 * @deprecated See ProductMapper.php
 */
class ProductIndexing extends ezfSolrDocumentFieldBase
{
    /**
     * Get data to index, and field name to use.
     * @since ez find 2.2: indexed values to be context aware different fields may be used for searching, sorting and faceting
     * @return array Associative array with field name and field value.
     *               Field value can be an array.
     * @see ezfSolrDocumentFieldBase::getData()
     */
    public function getData()
    {
        $data = parent::getData();
        $contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );
        if ($contentClassAttribute->Identifier === 'product') {
            $content = $this->ContentObjectAttribute->object();
            $container = ezpKernel::instance()->getServiceContainer();
            $helper = $container->get( 'myclient.product_helper' );
            $r = $helper->getSolrIndexData($content->ID);
            foreach ($r as $k => $v) {
                $fieldName = 'subattr_'.$k.'____t';
                $data[$fieldName] = $v;
            }
        }
        return $data;
    }
}

src/MyClient/LegacyBundle/ezpublish_legacy/myclient/settings/ezfind.ini.append.php

[SolrFieldMapSettings]
CustomMap[ezstring]=ProductIndexing

It worked very well. I could research the additional fields and they were taken into account in the text searches.

But that only works if I administer my products from the legacy BO.
If I want this information to be indexed when handling the product via the API or the new BO, do I have to make a Document Field Mappers?

Document Field Mappers
https://doc.ezplatform.com/en/1.13/guide/search/#document-field-mappers

Here is my Document Field Mappers and its configuration.

<?php
namespace MyClient\FrontEndBundle\Mapper;
use MyClient\FrontEndBundle\Helper\ProductHelper;
use EzSystems\EzPlatformSolrSearchEngine\FieldMapper\ContentFieldMapper;
use eZ\Publish\SPI\Persistence\Content\Handler as ContentHandler;
use eZ\Publish\SPI\Persistence\Content\Location\Handler as LocationHandler;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\SPI\Search;

class ProductMapper extends ContentFieldMapper
{
    protected $contentHandler;
    protected $locationHandler;
    protected $helper; // Product helper

    public function __construct(
        ContentHandler $contentHandler,
        LocationHandler $locationHandler,
        ProductHelper $helper
    ) {
        $this->contentHandler = $contentHandler;
        $this->locationHandler = $locationHandler;
        $this->helper = $helper;
    }

    public function accept(Content $content)
    {
        return $content->versionInfo->contentInfo->contentTypeId == 69;
    }

    public function mapFields(Content $content)
    {
        $l = $this->helper->getSolrIndexData($content->versionInfo->contentInfo->id);
        $return = [];
        foreach ($l as $k => $v) {
            $v = is_array($v) ? implode(', ', $v) : $v; // $v is an array... and must be a string... 
            $return[] = new Search\Field($k, $v, new Search\FieldType\StringField());
        }
        return $return;
    }
}
services:
    myclient.product_mapper:
        class: MyClient\FrontEndBundle\Mapper\ProductMapper
        arguments:
            - '@ezpublish.spi.persistence.content_handler'
            - '@ezpublish.spi.persistence.location_handler'
            - '@myclient.product_helper'
        tags:
            - {name: ezpublish.search.solr.field_mapper.content}

Is this the right step?

Because I do not find my values in SOLR.