Skip to content

Commit b9b802e

Browse files
committed
commit
1 parent eee9ebf commit b9b802e

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

app/base/tools/Search/Manager.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ public function ensureIndex(): bool
254254
return true;
255255
}
256256

257+
/**
258+
* Drops index
259+
*
260+
* @return static
261+
*/
262+
public function dropIndex() : static
263+
{
264+
try {
265+
$client = $this->getClient();
266+
$client->indices()->delete(['index' => $this->getIndexName()]);
267+
} catch (\Throwable $e) {}
268+
269+
return $this;
270+
}
271+
257272
/**
258273
* Prepares data from a frontend model to be indexed in Elasticsearch.
259274
*
@@ -449,18 +464,24 @@ public function searchData($page = 0, $pageSize = self::RESULTS_PER_PAGE, bool $
449464

450465
if ($onlyAggregations) {
451466
$searchParams['body']['size'] = 0;
467+
unset ($http_response_header['body']['from']);
468+
$searchParams['body']['aggs'] = $this->getAggregationsArray();
452469
} else {
453-
if (is_array($this->source)) {
454-
$searchParams['body']['_source'] = $this->source;
470+
if (is_array($this->getSource())) {
471+
$searchParams['body']['_source'] = $this->getSource();
455472
}
456473

457-
if (is_array($this->sort)) {
458-
$searchParams['body']['sort'] = $this->sort;
474+
if (is_array($this->getSort())) {
475+
$searchParams['body']['sort'] = $this->getSort();
459476
}
460477
}
461478

462479
$search_result = $this->getClient()->search($searchParams);
463480

481+
if ($onlyAggregations) {
482+
return $search_result['aggregations'];
483+
}
484+
464485
$total = $search_result['hits']['total']['value'] ?? 0;
465486
$hits = $search_result['hits']['hits'] ?? [];
466487
$docs = array_map(function ($el) {
@@ -470,6 +491,16 @@ public function searchData($page = 0, $pageSize = self::RESULTS_PER_PAGE, bool $
470491
return ['total' => $total, 'docs' => $docs];
471492
}
472493

494+
/**
495+
* return aggregated data
496+
*
497+
* @return array
498+
*/
499+
public function searchAggregatedData() : array
500+
{
501+
return $this->searchData(onlyAggregations: true);
502+
}
503+
473504
/**
474505
* Adds a condition for the "filter" clause of the Elasticsearch query.
475506
*
@@ -540,6 +571,28 @@ public function addGenericCondition(array $condition, string $boolType) : static
540571
return $this;
541572
}
542573

574+
/**
575+
* Sets sorting array
576+
*
577+
* @param array $sort Sort array
578+
* @return static
579+
*/
580+
public function setSort(array $sort) : static
581+
{
582+
$this->sort = $sort;
583+
return $this;
584+
}
585+
586+
/**
587+
* Gets sorting array
588+
*
589+
* @return array|null
590+
*/
591+
public function getSort() : ?array
592+
{
593+
return $this->sort;
594+
}
595+
543596
/**
544597
* Resets the sorting for the query.
545598
*

0 commit comments

Comments
 (0)