Skip to content

Elastic Search interferes with the default sort order of products (changing newest first to oldest first) #31043

Open
@zigojacko

Description

@zigojacko

Preconditions (*)

  1. Magento 2.4-develop
  2. 2.3.5-p1 is all I have tested this in so far.

Steps to reproduce (*)

  • Create a category in Magento with several products in it, all have the position in the category set to the value 1
    image

  • Go to category page

Expected result (*)

  1. The newest products in the database (the one with the highest entity_id) are shown first
    image
  2. The Order by for the SQL query for retrieving the products
 SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_category_product_index_store1` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id=33
 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.customer_group_id = 0 AND price_index.website_id = '1' ORDER BY `cat_index`.`position` asc, `e`.`entity_id` DESC

Actual result (*)

  1. The oldest products(with the lower entity_id) are shown first
    image
  2. The Order by for the SQL query for retrieving the products
SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, IFNULL(review_summary.reviews_count, 0) AS `reviews_count`, IFNULL(review_summary.rating_summary, 0) AS `rating_summary`, `stock_status_index`.`stock_status` AS `is_salable` FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.customer_group_id = 0 AND price_index.website_id = '1'
 LEFT JOIN `review_entity_summary` AS `review_summary` ON e.entity_id = review_summary.entity_pk_value AND review_summary.store_id = 1 AND review_summary.entity_type = (SELECT `review_entity`.`entity_id` FROM `review_entity` WHERE (entity_code = 'product'))
 INNER JOIN `cataloginventory_stock_status` AS `stock_status_index` ON e.entity_id = stock_status_index.product_id AND stock_status_index.website_id = 0 AND stock_status_index.stock_id = 1 WHERE (stock_status_index.stock_status = 1) AND (e.entity_id IN (1201, 1202, 1203)) ORDER BY FIELD(e.entity_id,1201,1202,1203)

⚠️ Note: In Magento 2.3.6 with Search Engine=MySQL the newest product in the database (the one with the highest entity_id) was shown first. With Magento 2.4-develop with Search Engine= Elasticsearch 7 this behaviour changed. The oldest product is shown first, the newest last.


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • 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”.

The severity of this issue will differ between niches but for some businesses, this is quite critical. Like designer or boutique fashion retailers where the target audience frequent the website to view the latest items to hit the fashion lines etc.


I can't believe there are not more reports of this... I did ask the question on Magento Stack Exchange and it is passed by will hardly any views and zero interaction.

I'm obviously assuming this is a bug and not meant to happen.

Activity

m2-assistant

m2-assistant commented on Nov 24, 2020

@m2-assistant

Hi @zigojacko. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


⚠️ 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, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

self-assigned this
on Jan 29, 2021
m2-assistant

m2-assistant commented on Jan 29, 2021

@m2-assistant

Hi @engcom-Alfa. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

    2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

    3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

    4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

    5. Add label Issue: Confirmed once verification is complete.

    6. Make sure that automatic system confirms that report has been added to the backlog.

removed their assignment
on Jan 29, 2021
self-assigned this
on Feb 3, 2021
m2-assistant

m2-assistant commented on Feb 3, 2021

@m2-assistant

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

    2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

    3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

    4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

    5. Add label Issue: Confirmed once verification is complete.

    6. Make sure that automatic system confirms that report has been added to the backlog.

engcom-Delta

engcom-Delta commented on Feb 3, 2021

@engcom-Delta
Contributor

Hi @zigojacko thank you for your report, I'm not able to reproduce issue by steps you described on clean 2.4-develop with elasticsearch
Manual testing scenario:

  • Install magento with elasticsearch
  • Set Price default sort order of products on category page
    image
  • Go to category page
    Result:
    ✔️ Products is sorted by price on category page
    Peek 2021-02-03 15-18
  • Go to Catalog->Categories
  • Set Display Settings->Default Product Listing Sort By=Product Name for another category
  • Go to category page

Result:
✔️ Products is sorted by product name on category page
Peek 2021-02-03 15-19

Please feel free to comment, reopen or create new ticket according to the Issue reporting guidelines
if you are still facing this issue on the latest 2.4-develop branch.

added
Reported on 2.3.5-p1Indicates original Magento version for the Issue report.
not-confirmedUse for Issue that was closed during confirmation
on Feb 3, 2021
removed their assignment
on Feb 3, 2021
zigojacko

zigojacko commented on Feb 3, 2021

@zigojacko
Author

@engcom-Delta you haven't understood the problem and don't show the testing of it.

This has been confirmed in a fresh install of Magento 2 and the problem is apparent.

It is also reported across the other web by other users.

The issue is not that sorting by attribute is not working at all. It is that the default sorting is reversed. So by default, sort order will be in the order that the products have been added (newest first) providing custom sort position not set in category.

Your test doesn't show this at all.

Please can you re-open and test for the actual issue?

zigojacko

zigojacko commented on Feb 3, 2021

@zigojacko
Author

As an example, see this demo store.

In this category, I just added a new product and set the same 'Position' for the product as all the others ('1').

You will see the product is at the end of the list. Even though it is newer than all the others.

If I then switch Elastic Search off and revert to the default MySQL, the newest product will switch to first in the list (as will the entire product listing reverse).

This is the problem @engcom-Delta

Please could someone re-open this issue?

31 remaining items

jigneshc3

jigneshc3 commented on May 5, 2023

@jigneshc3

The issue is still there. We have a site on Magento 2.4.5-p1 and we can still reproduce the issue. Its strange that after these many years, there is no proper solution found.

I already tried solutions give on #34502 and #32522 . Still no luck.

@zigojacko were you able to find solution yet? Would be great if you can share. Thanks.

Ahmet-Djedovic

Ahmet-Djedovic commented on May 7, 2023

@Ahmet-Djedovic

@jigneshc3 i have implemented a fix for my situation

Model:
MODULE_DIR/Model/CatalogSearch/ResourceModel/Fulltext

`<?php

namespace Ahmet\SortOrderAddon\Model\CatalogSearch\ResourceModel\Fulltext;

use Magento\Framework\App\ObjectManager;

class Collection extends \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection
{
protected function _renderFiltersBefore()
{
/**
* @var \Magento\Framework\App\RequestInterface $request
*/
if (isset($_SERVER['REQUEST_URI']) && !stristr($_SERVER['REQUEST_URI'], "catalogsearch")) {
$request = ObjectManager::getInstance()->create("\Magento\Framework\App\RequestInterface");
$attribute = $request->getParam("product_list_order", "position");
$direction = $request->getParam("product_list_dir", "asc");
if ($direction == 'asc') {
$this->addAttributeToSort($attribute, \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
} elseif ($direction == 'desc') {
$this->addAttributeToSort($attribute, \Magento\Framework\Data\Collection::SORT_ORDER_DESC);
}
}

    parent::_renderFiltersBefore();
}

}`

di.xml

`

<virtualType name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\CollectionFactory" type="Magento\Catalog\Model\ResourceModel\Product\CollectionFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">Ahmet\SortOrderAddon\Model\CatalogSearch\ResourceModel\Fulltext\Collection</argument>
    </arguments>
</virtualType>

<virtualType name="elasticsearchCategoryCollection" type="Ahmet\SortOrderAddon\Model\CatalogSearch\ResourceModel\Fulltext\Collection"/>
<virtualType name="elasticsearchFulltextSearchCollection" type="Ahmet\SortOrderAddon\Model\CatalogSearch\ResourceModel\Fulltext\Collection"/>

<preference for="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection" type="Ahmet\SortOrderAddon\Model\CatalogSearch\ResourceModel\Fulltext\Collection"/>

`

moved this to Pull Request In Progress in High Priority Backlogon Aug 19, 2024
engcom-Bravo

engcom-Bravo commented on Apr 9, 2025

@engcom-Bravo
Contributor

Hi @zigojacko,

Thanks for your reporting and collaboration.

We have tried to reproduce the issue in latest 2.4-develop instance and the issue is still reproducible.kindly refer the screenshots.

Image

Image

The oldest products(with the lower entity_id) are shown first.Hence confirming the issue.

Thanks.

added
Issue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmed
Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch
and removed
Issue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmed
Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch
on Apr 9, 2025
github-jira-sync-bot

github-jira-sync-bot commented on Apr 9, 2025

@github-jira-sync-bot

✅ Jira issue https://jira.corp.adobe.com/browse/AC-14411 is successfully created for this GitHub issue.

m2-assistant

m2-assistant commented on Apr 9, 2025

@m2-assistant

✅ Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: CatalogComponent: CatalogIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: PR in progressReported on 2.3.5-p1Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

Type

No type

Projects

Status

Pull Request In Progress

Milestone

No milestone

Relationships

None yet

    Participants

    @zigojacko@jigneshc3@magento-engcom-team@devchris79@engcom-Alfa

    Issue actions

      Elastic Search interferes with the default sort order of products (changing newest first to oldest first) · Issue #31043 · magento/magento2