Remove the possibility to add field with machine name id

Created on 31 August 2023, over 1 year ago
Updated 5 September 2023, over 1 year ago

Problem/Motivation

If a user adds a field with the machine name id that field will be used as an ID of items on the meilisearch index, this will break parts of the existing code.

Steps to reproduce

  1. Add Search API server, that connects to Meilisearch.
  2. Add index to the server.
  3. Add any field type from the data source and name it id
  4. Reindex by hand

Proposed resolution

So the original idea was to just use validation, but as @bcizej pointed out this could easily be bypassed and proposed a better solution of setting the primary key of index to something with search_api_ prefix. This is probablly the most elegant solution, since search api already implements validation for it.

This is related to 🐛 Primary key inference fails for Meilisearch >=1.x and fields ending in "id" (includes patch) Fixed and implicitly fixes it as well, in a different, better way.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

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

Comments & Activities

  • Issue created by @admirlju
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    102 pass
  • @admirlju opened merge request.
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇸🇮Slovenia bcizej

    Revert issue summary changes

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    102 pass
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    102 pass
  • Status changed to RTBC over 1 year ago
  • 🇸🇮Slovenia deaom

    The validation is limited only for meili search backend (tested with the db plugin) and it works. So you can not change the machine name of the field to id if the backed is meili, but can change and save if the backend is anything else, so marking it as RTBC.

  • Status changed to Needs work over 1 year ago
  • 🇸🇮Slovenia bcizej

    The error below appears when:

    1. Create an index or edit an index
    2. Select "- No server -" from server options
    3. Save the index
    4. Visit /admin/config/search/search-api/index/{index_name}/fields
    Error: Call to a member function getBackend() on null in search_api_meilisearch_form_search_api_index_fields_alter() (line 34 of modules/contrib/search_api_meilisearch/search_api_meilisearch.module).
    search_api_meilisearch_form_search_api_index_fields_alter(Array, Object, 'search_api_index_fields') (Line: 562)
    Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'search_api_index_fields') (Line: 840)
    Drupal\Core\Form\FormBuilder->prepareForm('search_api_index_fields', Array, Object) (Line: 284)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    

    Also form validation is not enough as the following is possible:

    1. Detach the index from meilisearch server (either to " - No server - " or other backend types) in the edit index form
    2. Save and then edit the fields and set the machine name as "id"
    3. Save and then reattach the index to meilisearch

    It should not be possible to have an index attached to meilisearch with an "id" field present. But perhaps we are overcomplicating things and should use search_api_ prefix (as these are reserved and user is not allowed to set them on any indexes) for primary key such as search_api_meilisearch_id or search_api_document_id. With 📌 Remove entity id mapping config Fixed close to finish a full reindex of meilisearch indexes will be required anyway...

  • @bcizej I like the idea you proposed of using search_api_ prefix. This is probably the most elegant way of fixing this. Will look in to it.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    102 pass
  • Status changed to Needs review over 1 year ago
  • Changed the logic to what @bcizej suggested, now the primary index id is search_api_document_id. Also setting the primary id at index creation prevents an error of multiple id when meilisearch is trying to guess what the primary id is at creation.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    102 pass
  • Status changed to Fixed over 1 year ago
  • 🇸🇮Slovenia bcizej

    Merged, thanks.

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

Production build 0.71.5 2024