Skip to content

Can't add a custom field resolver for ElasticSearch #39895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 5 tasks
Axel29 opened this issue May 7, 2025 · 1 comment
Open
1 of 5 tasks

Can't add a custom field resolver for ElasticSearch #39895

Axel29 opened this issue May 7, 2025 · 1 comment
Labels
Issue: ready for confirmation Reported on 2.4.5-p12 Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@Axel29
Copy link

Axel29 commented May 7, 2025

Preconditions and environment

  • Magento version : 2.4.5-p12
  • PHP 8.1.1
  • MySQL
  • ElasticSearch 7.11.2

Trying to add a custom data to the products index, but the custom field resolver is placed after the default one in the $items property of the \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver, making it unusable.

Steps to reproduce

  1. Create a module that depends on Magento_CatalogSearch and Magento_Elasticsearch (module.xml)
  2. Create a etc/di.xml file that adds a new product data (example below) and set the sortOrder before the default resolver:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="additionalFieldsProviderForElasticsearch" type="Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProvider">
        <arguments>
            <argument name="fieldsProviders" xsi:type="array">
                <item name="customPrice" xsi:type="object" sortOrder="31">Vendor\Module\Model\Adapter\BatchDataMapper\CustomPriceProvider</item>
            </argument>
        </arguments>
    </virtualType>

    <type name="Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\CompositeFieldProvider">
        <arguments>
            <argument name="providers" xsi:type="array">
                <item name="custom_price_sort" xsi:type="object">Vendor\Module\Model\Elasticsearch\Adapter\FieldMapper\CustomPriceField</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver">
        <arguments>
            <argument name="items" xsi:type="array">
                <item name="customPrice" xsi:type="object" sortOrder="31">Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice</item>
            </argument>
        </arguments>
    </type>
</config>
  1. Create the customPrice provider and fieldname resolver classes
  2. Use the \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface to get the field name in the Provider (example below):
/** @var \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider $this->attributeAdapterProvider */
$customPriceAttribute  = $this->attributeAdapterProvider->getByAttributeCode('custom_price');

$customPriceFieldName  = $this->fieldNameResolver->getFieldName(
    $customPriceAttribute,
    ['customerGroupId' => $customerGroupId, 'websiteId' => $websiteId],
);

Expected result

  1. The \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver::getFieldName uses the customPrice field name resolver
  2. The Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice is used when called by the Provider.

Actual result

  1. The default field name resolver is used

Additional information

I also noticed thanks to xDebug that we enter the \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver::__construct 5 times, the sort order is correct for each times except for the second entry.

Finally, I tried to declare the field name resolver in evey di.xml declarations for modules elasticsaerch, elasticsearch5, elasticsearch6 and elasticsearch7 (see below) but it still doesn't work.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="additionalFieldsProviderForElasticsearch" type="Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProvider">
        <arguments>
            <argument name="fieldsProviders" xsi:type="array">
                <item name="customPrice" xsi:type="object">Vendor\Module\Model\Adapter\BatchDataMapper\CustomPriceProvider</item>
            </argument>
        </arguments>
    </virtualType>

    <type name="Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\CompositeFieldProvider">
        <arguments>
            <argument name="providers" xsi:type="array">
                <item name="custom_price_sort" xsi:type="object">Vendor\Module\Model\Elasticsearch\Adapter\FieldMapper\CustomPriceField</item>
            </argument>
        </arguments>
    </type>
    <type name="Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver">
        <arguments>
            <argument name="items" xsi:type="array">
                <item name="customPrice" xsi:type="object">Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice</item>
            </argument>
        </arguments>
    </type>
    <virtualType name="elasticsearch5FieldNameResolver" type="\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver">
        <arguments>
            <argument name="items" xsi:type="array">
                <item name="customPrice" xsi:type="object" sortOrder="31">Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="elasticsearch6FieldNameResolver" type="\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver">
        <arguments>
            <argument name="items" xsi:type="array">
                <item name="customPrice" xsi:type="object" sortOrder="31">Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="\Magento\Elasticsearch7\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver" type="\Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CompositeResolver">
        <arguments>
            <argument name="items" xsi:type="array">
                <item name="customPrice" xsi:type="object" sortOrder="31">Vendor\Module\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\CustomPrice</item>
            </argument>
        </arguments>
    </virtualType>
</config>

Related to #32742 but this doesn't seem to work as expected.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Copy link

m2-assistant bot commented May 7, 2025

Hi @Axel29. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@github-project-automation github-project-automation bot moved this to Ready for Confirmation in Issue Confirmation and Triage Board May 7, 2025
@engcom-November engcom-November added Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it Reported on 2.4.5-p12 Indicates original Magento version for the Issue report. labels May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: ready for confirmation Reported on 2.4.5-p12 Indicates original Magento version for the Issue report. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Ready for Confirmation
Development

No branches or pull requests

2 participants