From ec3c6757e1f18b38f2989844b2a5f1d2b0786f75 Mon Sep 17 00:00:00 2001 From: Jaspar Gupta Date: Mon, 10 Dec 2018 09:43:17 +0000 Subject: [PATCH] feat: extra small features (#69) * feat: filter interface * feat: scopeFilter method in Searchable trait * feat: count method and filter method annotation * feat: ability to set routing for query * style: code format * refactor: use count method in pagination method * chore: revert to original method call feat: count method feat: FilterInterface --- src/Contracts/FilterInterface.php | 19 +++++++++++++++++++ src/EloquentBuilder.php | 21 ++++++++++++++++++++- src/QueryBuilder.php | 21 +++++++++++++++++++++ src/QueryGrammar.php | 14 +++++++++++--- src/Searchable.php | 24 +++++++++++++++++++----- 5 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 src/Contracts/FilterInterface.php diff --git a/src/Contracts/FilterInterface.php b/src/Contracts/FilterInterface.php new file mode 100644 index 0000000..65c0708 --- /dev/null +++ b/src/Contracts/FilterInterface.php @@ -0,0 +1,19 @@ +getModel()->newCollection($models); } + /** + * @param string $columns + * @return int + */ + public function count($columns = '*'): int + { + return $this->toBase()->getCountForPagination($columns); + } + + /** + * @param string $collectionClass + * @return Collection + */ public function getAggregations(string $collectionClass = ''): Collection { $collectionClass = $collectionClass ?: Collection::class; @@ -116,7 +135,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $results = $this->forPage($page, $perPage)->get($columns); - $total = $this->toBase()->getCountForPagination(); + $total = $this->toBase()->getCountForPagination($columns); return new LengthAwarePaginator($results, $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 0e9a13a..16d72c8 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -32,6 +32,8 @@ class QueryBuilder extends BaseBuilder protected $rawResponse; + protected $routing; + /** * All of the supported clause operators. * @@ -76,6 +78,25 @@ public function getParentId(): ?string return $this->parentId; } + /** + * @param string $routing + * @return QueryBuilder + */ + public function routing(string $routing): self + { + $this->routing = $routing; + + return $this; + } + + /** + * @return string|null + */ + public function getRouting(): ?string + { + return $this->routing; + } + /** * Add a where between statement to the query. * diff --git a/src/QueryGrammar.php b/src/QueryGrammar.php index 838a5eb..cb6b23f 100644 --- a/src/QueryGrammar.php +++ b/src/QueryGrammar.php @@ -19,7 +19,7 @@ class QueryGrammar extends BaseGrammar /** * Compile a select statement * - * @param Builder $builder + * @param Builder|QueryBuilder $builder * @return array */ public function compileSelect(Builder $builder): array @@ -1027,7 +1027,7 @@ protected function compileOrders(Builder $builder, $orders = []): array /** * Compile the given values to an Elasticsearch insert statement * - * @param Builder $builder + * @param Builder|QueryBuilder $builder * @param array $values * @return array */ @@ -1059,6 +1059,10 @@ public function compileInsert(Builder $builder, array $values): array '_id' => $doc['id'], ]; + if ($routing = $builder->getRouting()) { + $index['routing'] = $routing; + } + if ($parentId = $builder->getParentId()) { $index['parent'] = $parentId; } else if (isset($doc['_parent'])) { @@ -1079,7 +1083,7 @@ public function compileInsert(Builder $builder, array $values): array /** * Compile a delete query * - * @param Builder $builder + * @param Builder|QueryBuilder $builder * @return array */ public function compileDelete(Builder $builder): array @@ -1090,6 +1094,10 @@ public function compileDelete(Builder $builder): array 'id' => (string) $builder->wheres[0]['value'] ]; + if ($routing = $builder->getRouting()) { + $params['routing'] = $routing; + } + if ($parentId = $builder->getParentId()) { $params['parent'] = $parentId; } diff --git a/src/Searchable.php b/src/Searchable.php index 1a8ac34..ceaf0c3 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -2,6 +2,10 @@ namespace DesignMyNight\Elasticsearch; +use DesignMyNight\Elasticsearch\Contracts\FilterInterface; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; + trait Searchable { public static function getElasticsearchConnectionName(): string @@ -148,11 +152,11 @@ public function datesToSearchable(array $array): array foreach ($this->getArrayableRelations() as $key => $value) { $attributeName = snake_case($key); - if (isset($array[$attributeName]) && $value instanceof \Illuminate\Database\Eloquent\Model) { + if (isset($array[$attributeName]) && $value instanceof Model) { $array[$attributeName] = $value->datesToSearchable($array[$attributeName]); } else if (isset($array[$attributeName]) && $value instanceof \Illuminate\Support\Collection) { $array[$attributeName] = $value->map(function ($item, $i) use ($array, $attributeName) { - if ($item instanceof \Illuminate\Database\Eloquent\Model) { + if ($item instanceof Model) { return $item->datesToSearchable($array[$attributeName][$i]); } @@ -167,7 +171,7 @@ public function datesToSearchable(array $array): array /** * Convert a DateTime to a string in ES format. * - * @param \DateTime|int $value + * @param \DateTime|int $value * @return string */ public function fromDateTimeSearchable($value): string @@ -204,13 +208,23 @@ public function getSubDocumentIndexData($document) * New Collection * * @param array $models - * @return DesignMyNight\Elasticsearch\Collection + * @return Collection */ - public function newCollection(array $models = array()) + public function newCollection(array $models = []) { return new Collection($models); } + /** + * @param Builder $query + * @param FilterInterface $filters + * @return Builder + */ + public function scopeFilter(Builder $query, FilterInterface $filters): Builder + { + return $filters->apply($query); + } + public static function newElasticsearchQuery(): EloquentBuilder { $model = new static();