DB Location & AND Facets

Created on 8 October 2023, 9 months ago
Updated 21 October 2023, 8 months ago

Problem/Motivation

Search API queries containing both AND'd facets and a search_api_location option will cause a DB error.

Steps to reproduce

Create index to with a location (lat/lon) field, also add a field for a facet to be configured as AND. Complete a search including search_api_location option (for example using the search_api_location_views module - but this is completely not required - I discovered the original adding the location to the query myself).

Outline of reason for error

When retrieving the AND (not OR) facets a temporary table is made https://git.drupalcode.org/project/search_api/-/blob/8.x-1.x/modules/sea...
The method to do this removes all fields except item_id and unsets the expressions https://git.drupalcode.org/project/search_api/-/blob/8.x-1.x/modules/sea... it does not remove conditions, or having, leaving a $db_query at https://git.drupalcode.org/project/search_api/-/blob/8.x-1.x/modules/sea... of

"SELECT DISTINCT "t"."item_id" AS "item_id"
FROM
{search_api_db_locations} "t"
HAVING "location__distance" < :db_condition_placeholder_0"

Unsetting the expression means that the location__distance field defined in the having isn't in the query. Exception. Leaving the expression intact causes the score to be added in addition to the location__distance field.

"SELECT DISTINCT "t"."item_id" AS "item_id", :score AS "score", ST_Distance_Sphere(Point(:centre_lon, :centre_lat), ST_PointFromText(t.location)) / 1000 AS "location__distance"
FROM
{search_api_db_locations} "t"
HAVING "location__distance" < :db_condition_placeholder_0"

Proposed resolution

Question: What is the required for the temporary table?

Does it include the conditions (these haven't been unset), in which case it should include the having. If this is the case, but other expressions must be removed (for performance reasons? I'm guessing I'd need to understand exactly how the AND facet queries are constructed), then edge casing the location expression seems like the only option.

If the temporary table could include the expressions then just removing the lines that make them an empty array round line 2893 is simpler.

If there aren't supposed to be conditions, unlikely, the having could be removed.

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Database backend

Created by

πŸ‡³πŸ‡±Netherlands ekes

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

Comments & Activities

Production build 0.69.0 2024