diff --git a/src/ElasticquentResultCollection.php b/src/ElasticquentResultCollection.php index 21010c2..92d5f23 100644 --- a/src/ElasticquentResultCollection.php +++ b/src/ElasticquentResultCollection.php @@ -116,6 +116,16 @@ public function getHits() return $this->hits; } + /** + * Get Items + * + * @return array + */ + public function getItems() + { + return $this->items; + } + /** * Get aggregations * diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index 55400fd..63cb6d2 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -280,6 +280,29 @@ public static function complexSearch($params) return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static); } + /** + * Perform a "complex" or custom search and paginate result collection. + * + * Using this method, a custom query can be sent to Elasticsearch. + * + * @param $params + * @return ElasticquentResultCollection + */ + public static function complexSearchAndPaginate($params, $size = 10) + { + $instance = new static; + + $page = \Elasticquent\ElasticquentPaginator::resolveCurrentPage() ?: 1; + + $params["body"]["size"] = $size; + $params["body"]["from"] = ($page - 1) * $size; + + $result = $instance->getElasticSearchClient()->search($params); + $collection = new \Elasticquent\ElasticquentResultCollection($result, $instance = new static); + + return new \Elasticquent\ElasticquentPaginator($collection->getItems(), $collection->getHits(), $collection->totalHits(), $size, $page, ['path' => \Elasticquent\ElasticquentPaginator::resolveCurrentPath()]); + } + /** * Search *