Add ability to disable field data extraction in endpoint response

Created on 12 March 2024, 7 months ago
Updated 5 April 2024, 6 months ago

Problem/Motivation

The endpoint response is being unnecessarily slowed down when using solr with the "Retrieve result data from Solr" server config option enabled because the endpoint controller retrieves the fields from each item in the response using $item->getFields().(https://git.drupalcode.org/project/search_api_decoupled/-/blob/1.x/src/C...) Using this approach for retrieving the fields causes search_api to try and match fields by property path and if no match is found it loads data directly from the original entity, which incurs a database call to load the every entity in the result set.

Recently on a search that needs to return ~500 results I spent some time trying to configure my search server and index correctly to avoid these entity loads by was ultimately unable to convince search_api that the solr response using "Retrieve result data from Solr" had all the data I needed. Looking at the getFields() function docs it accepts an argument to disable extraction from original entities

  /**
   * Returns the item's fields.
   *
   * @param bool $extract
   *   (optional) If FALSE, only returns the fields that were previously set or
   *   extracted. Defaults to extracting the fields from the original object if
   *   necessary.
   *
   * @return \Drupal\search_api\Item\FieldInterface[]
   *   An array with the fields of this item, keyed by field identifier.
   */
  public function getFields($extract = TRUE);

By editing the SearchApiEndpointController and setting $item->getFields(FALSE) endpoint response times for 434 results went from ~1200ms to ~700ms.

Proposed resolution

Since the goal of this module is to provide a decoupled endpoint that talks directly with search indexes I think it makes sense that the default settings assume the index can return the necessary result data without entity loads. In this case that would mean adding a configuration to endpoints that controls the value set to getFields() and should default to not extract data when creating the response.

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ctrladel North Carolina, USA

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024