No results when a key consists only of a backslash

Created on 6 November 2024, about 2 months ago

Setup

  • Solr version: 8.7.0 and 8.11.2
  • Drupal Core version: 10.2.3
  • Search API version: 1.31.0
  • Search API Solr version: 4.3.2
  • Configured Solr Connector: Standard

Issue

When searching for e.g. "foo \" the results are always empty as a "SpanQuery is null" exception happens on the Solr server. I was able to track it down to \Drupal\search_api_solr\Utility\Utility::flattenKeysToPayloadScore where I noticed that first the keys are escaped

$k[] = $queryHelper->escapePhrase(trim($key));

and then, at the end of the method, the escaped keys are length-checked.

// See the boost_term_payload field type in schema.xml. If we send shorter
// or larger keys then defined by solr.LengthFilterFactory we'll trigger a
// "SpanQuery is null" exception.
$k = array_filter($k, function ($v) {
  $v = trim($v, '"');
  return (mb_strlen($v) >= 2) && (mb_strlen($v) <= 100);
});

We were able to work around this by patching in a line which un-escapes the keys in the filter function.

$k = array_filter($k, function ($v) {
  $v = trim($v, '"');
  $v = preg_replace('/\\\\("|\\\\)/', '$1', $v);
  return (mb_strlen($v) >= 2) && (mb_strlen($v) <= 100);
});
🐛 Bug report
Status

Active

Version

4.3

Component

Code

Created by

🇦🇹Austria castanearie

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

Comments & Activities

Production build 0.71.5 2024