Skip to content
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

Fixed Issue #17295 : Search REST API returns wrong total_count #20253

Closed
wants to merge 2 commits into from
Closed

Fixed Issue #17295 : Search REST API returns wrong total_count #20253

wants to merge 2 commits into from

Conversation

ronak2ram
Copy link
Member

Description (*)

I added function for getting total_count of result item.

Fixed Issues (if relevant)

  1. Search REST API returns wrong total_count #17295: Search REST API returns wrong total_count

Manual testing scenarios (*)

Request :

rest/V1/search?searchCriteria[requestName]=quick_search_container&searchCriteria[filterGroups][0][filters][0][field]=search_term&searchCriteria[filterGroups][0][filters][0][conditionType]=like&searchCriteria[filterGroups][0][filters][0][value]=test&searchCriteria[current_page]=1&searchCriteria[page_size]=2

Response :

{
    "items": [
        {
            "id": 126,
            "custom_attributes": [
                {
                    "attribute_code": "score",
                    "value": "73.7660923004150400"
                }
            ]
        },
        {
            "id": 61,
            "custom_attributes": [
                {
                    "attribute_code": "score",
                    "value": "73.7660923004150400"
                }
            ]
        }
    ],
    "aggregations": {
        "buckets": [
            {
                "name": "category_bucket",
                "values": [
                    {
                        "value": "32",
                        "metrics": [
                            "32",
                            "1"
                        ]
                    }
                ]
            },
            {
                "name": "stock_status_bucket",
                "values": []
            },
            {
                "name": "rating_summary_bucket",
                "values": []
            },
            {
                "name": "am_is_new_bucket",
                "values": []
            },
            {
                "name": "am_on_sale_bucket",
                "values": []
            },
            {
                "name": "color_bucket",
                "values": []
            },
            {
                "name": "sub_category_bucket",
                "values": []
            }
        ],
        "bucket_names": [
            "category_bucket",
            "stock_status_bucket",
            "rating_summary_bucket",
            "am_is_new_bucket",
            "am_on_sale_bucket",
            "color_bucket",
            "sub_category_bucket"
        ]
    },
    "search_criteria": {
        "request_name": "quick_search_container",
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "search_term",
                        "value": "test",
                        "condition_type": "like"
                    }
                ]
            }
        ],
        "page_size": 2,
        "current_page": 1
    },
    "total_count": 9
}

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-engcom-team
Copy link
Contributor

Hi @ronak2ram. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

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

@ronak2ram
Copy link
Member Author

@magento-engcom-team give me test instance

@magento-engcom-team
Copy link
Contributor

Hi @ronak2ram. Thank you for your request. I'm working on Magento instance for you

@magento-engcom-team
Copy link
Contributor

Hi @ronak2ram, here is your new Magento instance.
Admin access: https://pr-20253.instances.magento-community.engineering/admin
Login: admin Password: 123123q

@ronak2ram
Copy link
Member Author

Confirmed on the test instance.

@davidverholen
Copy link
Member

@ronak2ram currently your solution introduces a huge performance issue by sending a second search on each request to the search engine.

I did not go into detail yet, but wouldn't it be possible, to resolve this issue by fixing the original call to the search engine so it calculates the correct total count?

*/
public function getTotalCount(SearchCriteriaInterface $searchCriteria)
{
$requestBuilder = \Magento\Framework\App\ObjectManager::getInstance()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ObjectManager must not be used directly here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidverholen I will remove ObjectManager in next commit.

}

$request = $requestBuilder->create();
$searchResponse = $this->searchEngine->search($request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the search engine should not require a second search just to figure out the correct count

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not find any other solution can you please suggest me a better solution to eliminate searchEngine?

@ronak2ram
Copy link
Member Author

@magento-engcom-team give me test instance

@magento-engcom-team
Copy link
Contributor

Hi @ronak2ram. Thank you for your request. I'm working on Magento instance for you

@magento-engcom-team
Copy link
Contributor

Hi @ronak2ram, here is your new Magento instance.
Admin access: https://pr-20253.instances.magento-community.engineering/admin
Login: admin Password: 123123q

@ronak2ram ronak2ram requested a review from orlangur January 22, 2019 13:14
@ronak2ram
Copy link
Member Author

ronak2ram commented Jan 23, 2019

Hello @davidverholen @orlangur
Can you please review this pull. I changed the flow to the resolved issue.

@davidverholen
Copy link
Member

Hi @ronak2ram ,

this solution seems to be more "On the point" since it mostly extends the mysql search adapter to reflect the correct total count (even if the page size is lower than the total count).

However, you are introducing a new public method in the Query Response of the search framework which then probably needs to be implemented in all additional Search engines (like elasticsearch and 3rd party modules).

I will try to investigate a bit deeper if I see a solution, without changing the public api of the search framework. If not, it seems to be a design problem of the search framework api, which seems to be missing exactly the method you introduced in your change.

@ronak2ram
Copy link
Member Author

Hello @davidverholen
Is there any update regarding this PR?

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ronak2ram thanks for the pull request, please see my code review comments.

@buskamuza @melnikovi please review this contribution, in order to process it we'll need an approval of adding \Magento\Framework\Search\Response\QueryResponse::getTotalCount public method.

*
* @var array
*/
protected $countSqlSkipParts = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the access modifier to private

/**
* {@inheritdoc}
*/
public function getTotalCount()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@buskamuza @melnikovi can you please take a look if we can allow adding this method to the framework. It appears there is no other way to return the total count of the search result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class has count method already, that seem to have similar purpose. Are there cases when count that it uses is not efficient?

Copy link
Member Author

@ronak2ram ronak2ram Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melnikovi Count method returns a total count of the search result items of the current page.
We make getTotalCount for the return all pages items count.

*
* @return Select
*/
protected function getSelectCountSql($query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change method access modifier to private

*
* @return int
*/
public function getSize($query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change method access modifier to private

*
* @var int
*/
protected $totalCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change property access modifier to private

/**
* @param Document[] $documents
* @param AggregationInterface $aggregations
* @param Total size int $size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the phpdoc: @param int $size Total size

@ghost ghost assigned sivaschenko Mar 7, 2019
@orlangur orlangur removed their request for review March 7, 2019 16:42
@ronak2ram
Copy link
Member Author

Hello Masters @sivaschenko @melnikovi @davidverholen
Thanks for the review of this PR.
By mistake, I deleted the base branch of this pull request. So I can't able to update the changes in this PR.
So I created new PR with the suggested changes by @sivaschenko and latest code.

Please Review PR : #21713

@sivaschenko
Copy link
Member

Thanks @ronak2ram ! As all the concerns in the review were fixed, I approved the new pull request, closing this one as outdated.

@ghost
Copy link

ghost commented Mar 12, 2019

Hi @ronak2ram, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

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

Successfully merging this pull request may close these issues.

5 participants