Fix entity loading in SearchApiFieldTrait with undefined properties on the search results

Created on 15 August 2024, 4 months ago
Updated 19 August 2024, 4 months ago

Problem/Motivation

Entity loading is being performed on properties that are not defined on all bundles/types.

Steps to reproduce

  • Setup a content type with an entity reference field with an target type to taxonomy term.
  • Setup another content type without a entity reference field.
  • Create some dummy node's for both CT's.
  • Add both CT's to the search api index.
  • Add the associated entity reference field to the index.
  • Index items.
  • Create a REST export view and add the associated reference field (indexed).
  • Don't enable field rendering on the view field
  • Put a breakpoint with Xdebug on SearchApiFieldTrait.php:568.
  • And optional breakpoint at: SqlContentEntityStorage.php::getFromStorage.
  • When you perform a request on the REST export and start debugging, you will notice that a SQL request is being made for the nodes that don't have the entity reference field defined.

Proposed resolution

Entity loading should be done only if the property on the view result exist and don't have a value.

Feature request
Status

Closed: duplicate

Version

1.0

Component

Views integration

Created by

🇳🇱Netherlands bojan_dev

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @bojan_dev
  • Status changed to Needs review 4 months ago
  • Pipeline finished with Failed
    4 months ago
    Total: 302s
    #255008
  • Status changed to Needs work 4 months ago
  • 🇦🇹Austria drunken monkey Vienna, Austria

    Thanks for suggesting this improvement.

    You’re right that it doesn’t make sense to load entities just because they are missing fields that cannot have a value anyways. However, I fear that it’s a lot trickier to determine whether that is indeed the case than the simple check in your MR. In that line, $row is just a \Drupal\search_api\Plugin\views\ResultRow object, so will hardly ever have a $row->{$dependent} property. (This is probably also why the tests are failing.)

    I think to properly check whether the row might have the property in question, you will always need to load the entity.

    I also don’t quite understand your use case, to be honest. If your view only lists that single field, why include nodes that don’t have it at all? Why not add a “associated reference field: not empty” filter to the view?

  • 🇳🇱Netherlands bojan_dev

    I think to properly check whether the row might have the property in question, you will always need to load the entity

    I rather find a solution to not load entities, this makes even more sense when using Search API Solr, where you actually want to only use Solr instead of DB queries.

    I also don’t quite understand your use case, to be honest. If your view only lists that single field, why include nodes that don’t have it at all? Why not add a “associated reference field: not empty” filter to the view?

    For example imagine a specific CT that has tags or a category and you want to display the taxonomy label it in the search results. I don't want to add that filter because that would mean I won't get other CT's in the search. You need to see it as search results that for one or more CT's has more info to show.

  • Status changed to Closed: duplicate 4 months ago
  • 🇳🇱Netherlands bojan_dev

    Found related issue: Allow to index empty fields Active
    I will try to build the kill switch and contribute in the other issue.

Production build 0.71.5 2024