Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-29984: Add support for Field LIKE criterion with "*" wildcards #128

Merged
merged 4 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
EZP-29984: Field LIKE & CONTAINS criterion not behaving like SQL Sear…
…ch Engine

Adapt SolrSE to correct support for LIKE and CONTAINS.

Todo:
- Open PR with API and integration test coverage for this.
- CS / Adapt unit tests
  • Loading branch information
andrerom committed Feb 1, 2019
commit ffffc343bda8fbf6cc7c9e803f2810b4fb681acc
21 changes: 10 additions & 11 deletions lib/Query/Common/CriterionVisitor/Field/FieldIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @version //autogentag//
*/
namespace EzSystems\EzPlatformSolrSearchEngine\Query\Common\CriterionVisitor\Field;

Expand Down Expand Up @@ -42,8 +40,8 @@ public function canVisit(Criterion $criterion)
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target.
*
* @param Criterion $criterion
* @param CriterionVisitor $subVisitor
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
* @param \EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor $subVisitor
*
* @return string
*/
Expand All @@ -63,14 +61,15 @@ public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)

foreach ($searchFields as $name => $fieldType) {
foreach ($criterion->value as $value) {
$preparedValue = $this->escapeQuote(
$this->toString(
$this->mapSearchFieldValue($value, $fieldType)
),
true
);
$preparedValue = $this->toString($this->mapSearchFieldValue($value, $fieldType));

$queries[] = $name . ':"' . $preparedValue . '"';
if ($criterion->operator === Operator::CONTAINS) {
$preparedValue = $this->escapeWildcard($preparedValue);
$queries[] = $name . ':*' . $preparedValue . '*';
} else {
$preparedValue = $this->escapeQuote($preparedValue, true);
$queries[] = $name . ':"' . $preparedValue . '"';
}
}
}

Expand Down
27 changes: 7 additions & 20 deletions lib/Query/Common/CriterionVisitor/Field/FieldLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,20 @@ public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)

$queries = [];
foreach ($searchFields as $name => $fieldType) {
$preparedValue = $this->escape(
$preparedValue = $this->escapeWildcard(
$this->toString(
$this->mapSearchFieldValue($criterion->value, $fieldType)
)
);

$queries[] = $name . ':*' . $preparedValue . '*';
if (strpos($preparedValue, '%') !== false) {
$queries[] = $name . ':' . str_replace('%', '*', $preparedValue);
} else {
// BC with previus Solr Engine usage, however this is how CONTAINS should work, not LIKE
$queries[] = $name . ':*' . $preparedValue . '*';
}
}

return '(' . implode(' OR ', $queries) . ')';
}

/**
* Escapes value for use in wildcard search.
*
* @param $value
* @return mixed
*/
private function escape($value)
{
$reservedCharacters = preg_quote('+-&|!(){}[]^"~*?:\\ ');

return preg_replace_callback(
'/([' . $reservedCharacters . '])/',
function ($matches) {
return '\\' . $matches[0];
},
$value);
}
}
18 changes: 18 additions & 0 deletions lib/Query/CriterionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,22 @@ protected function escapeQuote($string, $doubleQuote = false)

return preg_replace($pattern, '\\\$1', $string);
}

/**
* Escapes value for use in wildcard search.
*
* @param $value
* @return mixed
*/
protected function escapeWildcard($value)
{
$reservedCharacters = preg_quote('+-&|!(){}[]^"~*?:\\ ');

return preg_replace_callback(
'/([' . $reservedCharacters . '])/',
function ($matches) {
return '\\' . $matches[0];
},
$value);
}
}