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

Bugfix: autocomplete suggests nonsearchable terms #102

Conversation

kuafucode
Copy link
Contributor

@kuafucode kuafucode commented Aug 27, 2024

Description (*)

Since 2.4.7 we noticed search autocomplete would suggest search terms with no result.

here are some more cases of bad search suggestions with sample data package :
Intending to search for "Didi Sports Watch"
"didi" -> suggestion "duti" with no matches

Intending to search for "Bottle"
"bot" -> suggestion for "but" and "boi", the latter bringing no search results

Intending to serach for "Summit
"summ" -> suggestion "seam" and "sued" which bring results but not the watch

Manual testing scenarios (*)

  1. install sample data package
  2. type in following terms in searchbox on store front and verify it don't suggest autocomplete with strange result:
  • "didi" -> does not suggestion "duti"
  • "bot" -> does not suggest "boi"
  • "summ" -> does notsuggestion "seam" or "sued"

Contribution checklist (*)

  • [x ] 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)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

@kuafucode kuafucode requested a review from a team as a code owner August 27, 2024 04:42
@Vinai
Copy link
Contributor

Vinai commented Aug 27, 2024

Thanks very much for the PR! I'll test it out today :)

@kuafucode
Copy link
Contributor Author

updated unitest, please check again

@rhoerr
Copy link
Contributor

rhoerr commented Aug 27, 2024

To check my understanding of the situation:

  • The issue is that the autocomplete suggestions were including fields that aren't actually searchable.
  • There isn't an existing field or flag in the context (app/code/Magento/Elasticsearch/Model/DataProvider/Base/Suggestions.php) that would allow us to limit it to searchable fields -- or maybe there is a searchable field but it doesn't give the data we need.
  • To address that, you added a new suggestible flag to the field mapping, and set that to the value of is_searchable from the underlying attribute.
  • Then you check that new suggestible flag when filtering fields for search suggestions, which limits it to values that will actually produce searchable matches.

Is that correct?

@kuafucode
Copy link
Contributor Author

kuafucode commented Aug 27, 2024

@rhoerr that's correct

There isn't an existing field or flag in the context (app/code/Magento/Elasticsearch/Model/DataProvider/Base/Suggestions.php) that would allow us to limit it to searchable fields -- or maybe there is a searchable field but it doesn't give the data we need.

There might be a way to retrieve the searchable flag by adding a dependency, but adding this flag to the field mapping is more natural and reliable to me.

@Vinai
Copy link
Contributor

Vinai commented Aug 28, 2024

I just ran bin/magento setup:upgrade while still on the branch, and I now get an error that seems related:

Running data recurring...{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"unknown parameter [suggestible] on mapper [custom_layout_update_file] of type [keyword]"}],"type":"mapper_parsing_exception","reason":"unknown parameter [suggestible] on mapper [custom_layout_update_file] of type [keyword]"},"status":400}

Commenting out

$fieldMapping[$fieldName]['suggestible'] = $attributeAdapter->isSuggestible();

and

$fieldMapping[$childFieldName]['suggestible'] = $attributeAdapter->isSuggestible();

in StaticField.php makes the error go away. Seems like that array is used in more ways maybe?

Do you have an idea?

@kuafucode
Copy link
Contributor Author

kuafucode commented Aug 30, 2024

Yes I was able to replicate the error. I thought the FieldProvider class I modified is a provider for in-memory mapping, turns out it is also used to build mapping for index, and we can not introduce a random property 'isSuggestible' just like that, opensearch won't take it.

If there is no way to get searchable flag from existing index, I might have to limit the modification within the Suggestions class, and retrieve searchable attributes from EAV.

My initial thought was to change isSearchable definition in vendor/magento/module-elasticsearch/Model/Adapter/FieldMapper/Product/AttributeAdapter.php to just

/**
   * Check if attribute is searchable.
   *
   * @return bool
   */
  public function isSearchable(): bool
  {
      return $this->getAttribute()->getIsSearchable();
  }

as it seems to me developers mixed up 'searchable' and 'indexible' overtime, but that might be a backward incompatible change.

@Vinai
Copy link
Contributor

Vinai commented Aug 30, 2024

Oh boy, mixing concerns indeed. I'm sorry I'm not familiar enough with the code to provide ideas for sensible workarounds. That said, I trust your technical prowess :)

@kuafucode
Copy link
Contributor Author

PR updated, please review

@kuafucode kuafucode requested a review from Vinai August 31, 2024 05:00
@rhoerr
Copy link
Contributor

rhoerr commented Sep 13, 2024

I requested small constructor changes for backwards compatibility, but otherwise it looks good to me. It's much less likely to cause problems with your refactor, thanks for doing that.

Copy link
Contributor

@Vinai Vinai left a comment

Choose a reason for hiding this comment

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

Besides the change request from @rhoerr to the constructor parameters, I have tested it and can confirm it works as intended! Also, setup:upgrade works again, too.

Thank you very much!

Copy link
Contributor

@rhoerr rhoerr left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thank you

@rhoerr
Copy link
Contributor

rhoerr commented Oct 1, 2024

I believe the unit test failures are unrelated and a result of the in-progress upstream transition to PHPUnit 10.

Since Vinai tested and approved other than the constructor change that is now done, I'm going to go ahead and merge this.

@rhoerr rhoerr merged commit 17c48fb into mage-os:2.4-develop Oct 1, 2024
5 of 7 checks passed
@kuafucode
Copy link
Contributor Author

@rhoerr I think it is still related to unit test for this change. I thought Suggestions is not a unitest covered class as I don't see unit test of this class in elasticsearch module, but it is actually in the elasticsearch7/opensearch module, we will have to look into those tests

rhoerr pushed a commit to rhoerr/mageos-magento2 that referenced this pull request Oct 29, 2024
* added additional check to ensure autocomplete suggests only searchable terms

---------

Co-authored-by: Ryan Sun <[email protected]>
@rhoerr rhoerr mentioned this pull request Oct 29, 2024
5 tasks
rhoerr added a commit that referenced this pull request Oct 30, 2024
* Bugfix: autocomplete suggests nonsearchable terms (#102)

* added additional check to ensure autocomplete suggests only searchable terms

---------

Co-authored-by: Ryan Sun <[email protected]>

* Update unit test for autocomplete suggests fix (#104)

* added additional check to ensure autocomplete suggests only searchable terms

* updated unitest for search suggest fix

* added type casting for isSuggestible

* reverted suggestible flag and fixed getSuggestFields method

* updated suggest contructor params to avoid bic

* updated unit test with new dependency productAttributeCollectionFactory for Suggestions

* add fieldProvider back to Suggestions constructor

---------

Co-authored-by: Ryan Sun <[email protected]>

---------

Co-authored-by: Ryan Sun <[email protected]>
Co-authored-by: Ryan Sun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants