add base authorization to person search + improve search ordering

This commit is contained in:
2021-11-15 11:17:03 +00:00
parent 4ba93bb709
commit 8296d60cb6
22 changed files with 317 additions and 151 deletions

View File

@@ -88,13 +88,13 @@ class SearchApi
private function buildCountQuery(array $queries, $types, $parameters)
{
$query = "SELECT COUNT(sq.key) AS count FROM ({union_unordered}) AS sq";
$query = "SELECT COUNT(*) AS count FROM ({union_unordered}) AS sq";
$unions = [];
$parameters = [];
foreach ($queries as $q) {
$unions[] = $q->buildQuery();
$parameters = \array_merge($parameters, $q->buildParameters());
$unions[] = $q->buildQuery(true);
$parameters = \array_merge($parameters, $q->buildParameters(true));
}
$unionUnordered = \implode(" UNION ", $unions);

View File

@@ -76,33 +76,58 @@ class SearchApiQuery
return $this;
}
public function buildQuery(): string
public function buildQuery(bool $countOnly = false): string
{
$where = \implode(' AND ', $this->whereClauses);
$isMultiple = count($this->whereClauses);
$where =
($isMultiple ? '(' : '').
\implode(
($isMultiple ? ')' : '').' AND '.($isMultiple ? '(' : '')
, $this->whereClauses).
($isMultiple ? ')' : '')
;
return \strtr("SELECT
if (!$countOnly) {
$select = \strtr("
'{key}' AS key,
{metadata} AS metadata,
{pertinence} AS pertinence
FROM {from}
WHERE {where}
", [
'{key}' => $this->selectKey,
'{metadata}' => $this->jsonbMetadata,
'{pertinence}' => $this->pertinence,
]);
} else {
$select = "1 AS c";
}
return \strtr("SELECT
{select}
FROM {from}
WHERE {where}
", [
'{key}' => $this->selectKey,
'{metadata}' => $this->jsonbMetadata,
'{pertinence}' => $this->pertinence,
'{select}' => $select,
'{from}' => $this->fromClause,
'{where}' => $where,
]);
}
public function buildParameters(): array
public function buildParameters(bool $countOnly = false): array
{
return \array_merge(
$this->selectKeyParams,
$this->jsonbMetadataParams,
$this->pertinenceParams,
$this->fromClauseParams,
\array_merge([], ...$this->whereClausesParams),
);
if (!$countOnly) {
return \array_merge(
$this->selectKeyParams,
$this->jsonbMetadataParams,
$this->pertinenceParams,
$this->fromClauseParams,
\array_merge([], ...$this->whereClausesParams),
);
} else {
return \array_merge(
$this->fromClauseParams,
\array_merge([], ...$this->whereClausesParams),
);
}
}
}

View File

@@ -30,7 +30,6 @@ trait PrepareClientTrait
*
* @param string $username the username (default 'center a_social')
* @param string $password the password (default 'password')
* @return \Symfony\Component\BrowserKit\Client
* @throws \LogicException
*/
public function getClientAuthenticated(

View File

@@ -20,7 +20,12 @@ class SearchApiQueryTest extends TestCase
$query = $q->buildQuery();
$this->assertStringContainsString('foo AND bar', $query);
$this->assertStringContainsString('(foo) AND (bar)', $query);
$this->assertEquals(['alpha', 'beta'], $q->buildParameters());
$query = $q->buildQuery(true);
$this->assertStringContainsString('(foo) AND (bar)', $query);
$this->assertEquals(['alpha', 'beta'], $q->buildParameters());
}