Skip to content

Commit

Permalink
feat: extra small features (#69)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Jaspar Gupta authored Dec 10, 2018
1 parent 08bdb27 commit ec3c675
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/Contracts/FilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DesignMyNight\Elasticsearch\Contracts;

use Illuminate\Database\Eloquent\Builder;

/**
* Interface FilterInterface
* @package DesignMyNight\Elasticsearch\Contracts
*/
interface FilterInterface
{
/**
* @param Builder $builder
*
* @return Builder
*/
public function apply(Builder $builder):Builder;
}
21 changes: 20 additions & 1 deletion src/EloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace DesignMyNight\Elasticsearch;

use DesignMyNight\Elasticsearch\Contracts\FilterInterface;
use Generator;
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;

/**
* Class EloquentBuilder
* @method QueryBuilder filter(FilterInterface $filters) Support for Searchable::scopeFilter()
* @package DesignMyNight\Elasticsearch
*/
class EloquentBuilder extends BaseBuilder
{
protected $type;
Expand Down Expand Up @@ -52,6 +58,19 @@ public function get($columns = ['*'])
return $builder->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;
Expand Down Expand Up @@ -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(),
Expand Down
21 changes: 21 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class QueryBuilder extends BaseBuilder

protected $rawResponse;

protected $routing;

/**
* All of the supported clause operators.
*
Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 11 additions & 3 deletions src/QueryGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
24 changes: 19 additions & 5 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ec3c675

Please sign in to comment.