@@ -254,6 +254,21 @@ public function ensureIndex(): bool
254
254
return true ;
255
255
}
256
256
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
+
257
272
/**
258
273
* Prepares data from a frontend model to be indexed in Elasticsearch.
259
274
*
@@ -449,18 +464,24 @@ public function searchData($page = 0, $pageSize = self::RESULTS_PER_PAGE, bool $
449
464
450
465
if ($ onlyAggregations ) {
451
466
$ searchParams ['body ' ]['size ' ] = 0 ;
467
+ unset ($ http_response_header ['body ' ]['from ' ]);
468
+ $ searchParams ['body ' ]['aggs ' ] = $ this ->getAggregationsArray ();
452
469
} 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 () ;
455
472
}
456
473
457
- if (is_array ($ this ->sort )) {
458
- $ searchParams ['body ' ]['sort ' ] = $ this ->sort ;
474
+ if (is_array ($ this ->getSort () )) {
475
+ $ searchParams ['body ' ]['sort ' ] = $ this ->getSort () ;
459
476
}
460
477
}
461
478
462
479
$ search_result = $ this ->getClient ()->search ($ searchParams );
463
480
481
+ if ($ onlyAggregations ) {
482
+ return $ search_result ['aggregations ' ];
483
+ }
484
+
464
485
$ total = $ search_result ['hits ' ]['total ' ]['value ' ] ?? 0 ;
465
486
$ hits = $ search_result ['hits ' ]['hits ' ] ?? [];
466
487
$ docs = array_map (function ($ el ) {
@@ -470,6 +491,16 @@ public function searchData($page = 0, $pageSize = self::RESULTS_PER_PAGE, bool $
470
491
return ['total ' => $ total , 'docs ' => $ docs ];
471
492
}
472
493
494
+ /**
495
+ * return aggregated data
496
+ *
497
+ * @return array
498
+ */
499
+ public function searchAggregatedData () : array
500
+ {
501
+ return $ this ->searchData (onlyAggregations: true );
502
+ }
503
+
473
504
/**
474
505
* Adds a condition for the "filter" clause of the Elasticsearch query.
475
506
*
@@ -540,6 +571,28 @@ public function addGenericCondition(array $condition, string $boolType) : static
540
571
return $ this ;
541
572
}
542
573
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
+
543
596
/**
544
597
* Resets the sorting for the query.
545
598
*
0 commit comments