adaptations for third party search

This commit is contained in:
2021-10-12 09:28:53 +02:00
parent f4369553e1
commit e9192c5011
5 changed files with 138 additions and 19 deletions

View File

@@ -27,7 +27,7 @@ class SearchUserApiProvider implements SearchApiInterface
->setSelectPertinence("GREATEST(SIMILARITY(LOWER(UNACCENT(?)), u.usernamecanonical),
SIMILARITY(LOWER(UNACCENT(?)), u.emailcanonical))", [ $pattern, $pattern ])
->setFromClause("users AS u")
->setWhereClause("SIMILARITY(LOWER(UNACCENT(?)), u.usernamecanonical) > 0.15
->setWhereClauses("SIMILARITY(LOWER(UNACCENT(?)), u.usernamecanonical) > 0.15
OR
SIMILARITY(LOWER(UNACCENT(?)), u.emailcanonical) > 0.15
", [ $pattern, $pattern ]);

View File

@@ -12,8 +12,8 @@ class SearchApiQuery
private array $pertinenceParams = [];
private ?string $fromClause = null;
private array $fromClauseParams = [];
private ?string $whereClause = null;
private array $whereClauseParams = [];
private array $whereClauses = [];
private array $whereClausesParams = [];
public function setSelectKey(string $selectKey, array $params = []): self
{
@@ -47,16 +47,39 @@ class SearchApiQuery
return $this;
}
public function setWhereClause(string $whereClause, array $params = []): self
/**
* Set the where clause and replace all existing ones.
*
*/
public function setWhereClauses(string $whereClause, array $params = []): self
{
$this->whereClause = $whereClause;
$this->whereClauseParams = $params;
$this->whereClauses = [$whereClause];
$this->whereClausesParams = [$params];
return $this;
}
/**
* Add a where clause.
*
* This will add to previous where clauses with and `AND` join
*
* @param string $whereClause
* @param array $params
* @return $this
*/
public function andWhereClause(string $whereClause, array $params = []): self
{
$this->whereClauses[] = $whereClause;
$this->whereClausesParams[] = $params;
return $this;
}
public function buildQuery(): string
{
$where = \implode(' AND ', $this->whereClauses);
return \strtr("SELECT
'{key}' AS key,
{metadata} AS metadata,
@@ -68,7 +91,7 @@ class SearchApiQuery
'{metadata}' => $this->jsonbMetadata,
'{pertinence}' => $this->pertinence,
'{from}' => $this->fromClause,
'{where}' => $this->whereClause,
'{where}' => $where,
]);
}
@@ -79,7 +102,7 @@ class SearchApiQuery
$this->jsonbMetadataParams,
$this->pertinenceParams,
$this->fromClauseParams,
$this->whereClauseParams,
\array_merge([], ...$this->whereClausesParams),
);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Search;
use Chill\MainBundle\Search\SearchApiQuery;
use PHPUnit\Framework\TestCase;
class SearchApiQueryTest extends TestCase
{
public function testMultipleWhereClauses()
{
$q = new SearchApiQuery();
$q->setSelectJsonbMetadata('boum')
->setSelectKey('bim')
->setSelectPertinence('1')
->setFromClause('badaboum')
->andWhereClause('foo', [ 'alpha' ])
->andWhereClause('bar', [ 'beta' ])
;
$query = $q->buildQuery();
$this->assertStringContainsString('foo AND bar', $query);
$this->assertEquals(['alpha', 'beta'], $q->buildParameters());
}
public function testWithoutWhereClause()
{
$q = new SearchApiQuery();
$q->setSelectJsonbMetadata('boum')
->setSelectKey('bim')
->setSelectPertinence('1')
->setFromClause('badaboum')
;
$this->assertTrue(\is_string($q->buildQuery()));
$this->assertEquals([], $q->buildParameters());
}
}