Skip to content

Commit

Permalink
Simplify querying. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajmundtoth0 authored Sep 27, 2024
1 parent 934c1c5 commit b2fbac9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 63 deletions.
17 changes: 12 additions & 5 deletions Tests/Feature/ElasticsearchAuditServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,19 @@ public function testSearch(): void
'name' => 'Not John Doe',
]);

$query = [
'bool' => [
'must' => [
[
'term' => [
'auditable_id' => $user->id,
],
],
],
],
];
$result = $service
->setDateRange(null)
->setDateRange(now()->subDays(1))
->setTerm('auditable_id', 314159)
->setTerm('auditable_id', $user->id)
->search();
->search(query: $query);

static::assertTrue($result->asBool());
static::assertSame($user->toArray(), $result->asArray());
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
"require": {
"php": ">=8.2",
"elasticsearch/elasticsearch": "^8.0",
"nyholm/psr7": "^1.8",
"owen-it/laravel-auditing": "^13.0",
"php-http/guzzle7-adapter": "^1.0",
"php-http/httplug": "^2.4"
},
"autoload": {
Expand All @@ -51,7 +49,8 @@
"php-http/mock-client": "^1.6",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "*",
"phpunit/phpunit": "^11.0"
"phpunit/phpunit": "^11.0",
"nyholm/psr7": "^1.8"
},
"config": {
"allow-plugins": {
Expand Down
60 changes: 5 additions & 55 deletions src/Services/ElasticsearchAuditService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace rajmundtoth0\AuditDriver\Services;

use Carbon\Carbon;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use OwenIt\Auditing\Contracts\Audit;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Contracts\AuditDriver;
Expand Down Expand Up @@ -44,11 +42,8 @@ class ElasticsearchAuditService implements AuditDriver
*/
public function __construct(
private readonly ElasticsearchClient $client,
/** @var array<string, mixed> $query */
private array $query = [],
) {
$this->loadConfigs();
$this->setBaseQuery();
$this->client->setClient();
}

Expand Down Expand Up @@ -81,22 +76,6 @@ private function loadConfigs(): void
$this->queueConnection = $queueConnection;
}

private function setBaseQuery(): void
{
$this->query = [
'index' => $this->index,
'type' => $this->auditType,
'body' => [
'query' => [
'bool' => [
'minimum_should_match' => 1,
],
],
'track_scores' => true,
],
];
}

/**
* @throws AuditDriverException
* @throws AuditingException
Expand Down Expand Up @@ -175,30 +154,6 @@ public function index(DocumentModel $document, bool $shouldReturnResult = false)
return $this->client->index($document, $shouldReturnResult);
}

public function setDateRange(
?Carbon $date,
string $name = 'created_at',
string $operator = 'gte',
): self {
if (!$date) {
return $this;
}
data_set($this->query, "body.query.bool.must.range.{$name}.{$operator}", $date->toDateTimeString());

return $this;
}

public function setTerm(string $name, int|string $value): self
{
Arr::set($this->query, 'body.query.bool.should', [
'term' => [
$name => $value,
],
]);

return $this;
}

/**
* @throws AuditDriverException
* @throws ClientResponseException
Expand Down Expand Up @@ -251,21 +206,16 @@ public function searchAuditDocument(
}

/**
* @param array<string, mixed> $query
*
* @throws AuditDriverException
* @throws ClientResponseException
* @throws NoNodeAvailableException
* @throws ServerResponseException
*/
public function search(
int $pageSize = 10_000,
?int $from = 0,
string $sort = 'desc',
): Elasticsearch {
data_set($this->query, 'size', $pageSize);
data_set($this->query, 'from', $from);
data_set($this->query, 'body.sort.created_at.order', $sort);

return $this->client->search($this->query);
public function search(array $query = []): Elasticsearch
{
return $this->client->search($query);
}

/**
Expand Down

0 comments on commit b2fbac9

Please sign in to comment.