Facets do not generate based on facet module settings

Created on 9 September 2023, about 1 year ago
Updated 30 September 2023, about 1 year ago

Problem/Motivation

Facets support was added in #3159003: Facet support however the current implementation is wrong. Facets are generated based on filterableAttributes but the facets should be generated based on how the user configures and which ones are attached to the view. The facets module attaches search_api_facets option to the search api query with all the information needed to build the proper facets information.

This might also be a candidate for a submodule search_api_meilisearch_facets so we can add dependency on facets module and users can enable the module if they wish to use facets.

Steps to reproduce

  • Install facets module
  • Create a view based on meilisearch index
  • Create a facet
  • Attach a facet to the view
  • Perform a search
  • Facets based on filterable attributes are displayed regardless of the actual facet settings

Proposed resolution

  • Move the facets handling to a submodule
  • Generate facets based on facets configuration received in the search api query via facets api

Remaining tasks

  • Write the code
  • Write automated tests
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇸🇮Slovenia bcizej

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bcizej
  • First commit to issue fork.
  • Status changed to Needs work about 1 year ago
  • Moved the facets logic to a submodule and using the search_api_facets option set by the facets module.

    But I don't understand this part

    Generate facets based on facets configuration received in the search api query via facets api

    since the same data is returned when correctly using facets attribute when searching. That data can then be passed into the query results and it will correctly format everything.

  • Figured out why this has to be done

    Generate facets based on facets configuration received in the search api query via facets api

    but this leads to a new problem, meilisearch facets can't be numerical.

    So let's say you are indexing a field that is a reference to some other data (example created by), by default you will only index reference id (in example uid), this field will be numerical.

    From documentation:

    Meilisearch does not support facet search on numbers. Convert numeric facets to strings to make them searchable.

    Internally, Meilisearch represents numbers as float64. This means they lack precision and can be represented in different ways, making it difficult to search facet values effectively.

    That said if admins change field type to string for ids, it works fine. So maybe there should be some documentation that explains this to users.

  • @admirlju opened merge request.
  • I misunderstood what @bcizej meant with this issue. He wanted like other backends to show all facet values even if the query and filter would result in zero hits, but the count next to it should show zero.

    Sadly Meilisearch doesn't have a good way of getting facets in this way, so for now, there must be two requests to facets API, but I have made a feature request for them to add this option.

  • Status changed to Needs review about 1 year ago
  • Added tests to the submodule. While the solution is not perfect, I hope the feature requests get accepted and it can be simplified.

  • Removed QueryPreExecuteSubscriber because there was no need to update any query configurations for the module to work correctly.

  • Status changed to Needs work about 1 year ago
  • Status changed to Needs review about 1 year ago
  • Using dependency injection for the service instead of extending and some CS fixes. Ready for review.

  • First commit to issue fork.
  • 🇸🇮Slovenia DeaOm

    The getting the config part was working only in the instance your server name was meilisearch, if you named it anything else, the config was empty and therefore an error was thrown as there was no backend_config which means the master key was set to null. Added a helper backend trait that basically gets the backend configuration to then set the meilisearch api with the backed configuration host and key.

    I did add the facets under filter criteria of the search view and tested the functionality and it seems it's working correctly. I added a fulltext type to a title in search api, the facets are also set up to a field title. If there is no term in the search box, all the facets are shown with the results in the brackets (basically all need to have a number higher then 0). Then once I enter a search term in the search box, I again get all the facets, but this time the brackets number changes to the "result hits".

  • Status changed to RTBC about 1 year ago
  • Tested the changes and everything is passing Since I only added @throws tags to a few PHPdocs, I'll change the status to RTBC.

  • Status changed to Needs work about 1 year ago
  • Status changed to Needs review about 1 year ago
  • Changed the mentioned problems. It needs to be reviewed again.

  • 🇸🇮Slovenia bcizej

    Branch rebased.

  • Status changed to Fixed about 1 year ago
  • 🇸🇮Slovenia bcizej

    Merged, thanks. Any issues regarding facets can use the "Facets" option from "Component" dropdown.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024