Skip to content
Snippets Groups Projects
Commit b8fa0545 authored by Demian Katz's avatar Demian Katz Committed by GitHub
Browse files

Add trailing question mark handling for eDismax. (#796)

parent ce403e8f
No related merge requests found
...@@ -154,19 +154,24 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -154,19 +154,24 @@ class QueryBuilder implements QueryBuilderInterface
$params->set('hl.q', $oldString); $params->set('hl.q', $oldString);
} }
} }
} else { } else if ($handler->hasDismax()) {
if ($handler->hasDismax()) { // If we're using extended dismax, we'll miss out on the question
$params->set('qf', implode(' ', $handler->getDismaxFields())); // mark fix in createAdvancedInnerSearchString(), so we should
$params->set('qt', $handler->getDismaxHandler()); // apply it here. If other query munges arise that are valuable
foreach ($handler->getDismaxParams() as $param) { // to both dismax and edismax, we should add a wrapper function
$params->add(reset($param), next($param)); // around them and call it from here instead of this one very
} // specific check.
if ($handler->hasFilterQuery()) { $string = $this->fixTrailingQuestionMarks($string);
$params->add('fq', $handler->getFilterQuery()); $params->set('qf', implode(' ', $handler->getDismaxFields()));
} $params->set('qt', $handler->getDismaxHandler());
} else { foreach ($handler->getDismaxParams() as $param) {
$string = $handler->createSimpleQueryString($string); $params->add(reset($param), next($param));
} }
if ($handler->hasFilterQuery()) {
$params->add('fq', $handler->getFilterQuery());
}
} else {
$string = $handler->createSimpleQueryString($string);
} }
} }
$params->set('q', $string); $params->set('q', $string);
...@@ -362,6 +367,28 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -362,6 +367,28 @@ class QueryBuilder implements QueryBuilderInterface
} }
} }
/**
* If the query ends in a non-escaped question mark, the user may not really
* intend to use the question mark as a wildcard -- let's account for that
* possibility.
*
* @param string $string Search query to adjust
*
* @return string
*/
protected function fixTrailingQuestionMarks($string)
{
if (substr($string, -1) == '?' && substr($string, -2) != '\?') {
// Make sure all question marks are properly escaped (first unescape
// any that are already escaped to prevent double-escapes, then escape
// all of them):
$strippedQuery
= str_replace('?', '\?', str_replace('\?', '?', $string));
$string = "({$string}) OR (" . $strippedQuery . ")";
}
return $string;
}
/** /**
* Return advanced inner search string based on input and handler. * Return advanced inner search string based on input and handler.
* *
...@@ -386,17 +413,8 @@ class QueryBuilder implements QueryBuilderInterface ...@@ -386,17 +413,8 @@ class QueryBuilder implements QueryBuilderInterface
return $string; return $string;
} }
// If the query ends in a non-escaped question mark, the user may not really // Account for trailing question marks:
// intend to use the question mark as a wildcard -- let's account for that $string = $this->fixTrailingQuestionMarks($string);
// possibility
if (substr($string, -1) == '?' && substr($string, -2) != '\?') {
// Make sure all question marks are properly escaped (first unescape
// any that are already escaped to prevent double-escapes, then escape
// all of them):
$strippedQuery
= str_replace('?', '\?', str_replace('\?', '?', $string));
$string = "({$string}) OR (" . $strippedQuery . ")";
}
return $handler return $handler
? $handler->createAdvancedQueryString($string, false) : $string; ? $handler->createAdvancedQueryString($string, false) : $string;
......
...@@ -129,6 +129,34 @@ class QueryBuilderTest extends \VuFindTest\Unit\TestCase ...@@ -129,6 +129,34 @@ class QueryBuilderTest extends \VuFindTest\Unit\TestCase
} }
} }
/**
* Test generation with a query handler with edismax
*
* @return void
*/
public function testQueryHandlerWithEdismax()
{
// Set up an array of expected inputs and outputs:
// @codingStandardsIgnoreStart
$tests = [
['this?', '(this?) OR (this\?)'],// trailing question mark
];
// @codingStandardsIgnoreEnd
$qb = new QueryBuilder(
[
'test' => ['DismaxHandler' => 'edismax', 'DismaxFields' => ['foo']]
]
);
foreach ($tests as $test) {
list($input, $output) = $test;
$q = new Query($input, 'test');
$response = $qb->build($q);
$processedQ = $response->get('q');
$this->assertEquals($output, $processedQ[0]);
}
}
/** /**
* Test that the appropriate handler gets called for a quoted search when exact * Test that the appropriate handler gets called for a quoted search when exact
* settings are enabled. * settings are enabled.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment