Skip to content

Commit

Permalink
Better namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Torann committed Jun 18, 2017
1 parent 29b8bb1 commit d3da9ff
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 90 deletions.
28 changes: 2 additions & 26 deletions config/cloud-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'config' => [
'endpoint' => env('CLOUDSEARCH_ENDPOINT'),
'region' => env('CLOUDSEARCH_REGION', 'us-east-1'),
'region' => env('CLOUDSEARCH_REGION'),

'credentials' => [
'key' => env('AWS_KEY'),
Expand All @@ -32,7 +32,7 @@
|
*/

'domain_name' => 'lulebe-staging',
'domain_name' => 'you-domain',

/*
|--------------------------------------------------------------------------
Expand All @@ -44,31 +44,7 @@
*/

'fields' => [

// General
'id' => 'literal',
'locale' => 'literal',
'category_id' => 'literal',
'status' => 'literal',
'slug' => 'literal',
'image_file_name' => 'literal',
'tags' => 'literal-array',

// Articles
'user_id' => 'literal',
'featured' => 'literal',
'title' => 'text',
'url' => 'literal',
'source' => 'text',
'excerpt' => 'text',

// Plants
'parent_id' => 'literal',
'name' => 'text',
'common_names' => 'text-array',
'botanical_name' => 'text',
'ph' => 'literal-array',
'zones' => 'literal-array',
],

/*
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ A configuration file will be publish to `config/cloud-search.php`.

The better help manage fields, the package ships with a simple field management command. This is completely optional, as you can manage them in the AWS console.

> **NOTE:** If you choose not to use this command to manage or setup your fields, you will still need to add the field `searchable_type` as a `literal`. This is used to store the model type.
They can be found in the `config/cloud-search.php` file under the `fields` property:

```php
Expand Down
49 changes: 15 additions & 34 deletions src/CloudSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use LaravelCloudSearch\Query\Builder;
use Aws\CloudSearch\CloudSearchClient;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Aws\CloudSearchDomain\CloudSearchDomainClient;
use Illuminate\Database\Eloquent\Relations\Relation;
use LaravelCloudSearch\Query\StructuredQueryBuilder;
use Aws\CloudSearchDomain\Exception\CloudSearchDomainException;

class CloudSearcher
Expand Down Expand Up @@ -74,7 +76,7 @@ public function update($models)
$payload->push([
'type' => 'add',
'id' => $model->getSearchableId(),
'fields' => array_map(function($value) {
'fields' => array_map(function ($value) {
return is_null($value) ? '' : $value;
}, $fields),
]);
Expand Down Expand Up @@ -128,19 +130,15 @@ public function remove($models)
*
* @param string $term
* @param int $perPage
* @param array $options
*
* @return LengthAwarePaginator|array
*/
public function quickSearch($term, $perPage = 10, array $options = [])
public function searchAll($term, $perPage = 15)
{
$results = $this->performSearch($term, array_merge([
'size' => $perPage,
], $options));

return Arr::get($options, 'group', false)
? $this->groupResults($results)
: $results;
return $this->newQuery()
->term($term)
->take($perPage)
->get();
}

/**
Expand Down Expand Up @@ -190,6 +188,7 @@ public function execute(StructuredQueryBuilder $builder)
}
catch (CloudSearchDomainException $e) {
dd($e->getAwsErrorMessage() ?: $e->getMessage());

return $e->getAwsErrorMessage() ?: $e->getMessage();
}
}
Expand All @@ -210,31 +209,13 @@ protected function hydrateResults(array $items)
return Collection::make($items);
}

/**
* Group Elasticsearch results by table name.
*
* @param Collection $results
*
* @return array
*/
protected function groupResults(Collection $results)
{
$groups = [];

$results->each(function ($item) use (&$groups) {
$groups[$item->getTable()][] = $item;
});

return $groups;
}

/**
* Paginate the given query into a simple paginator.
*
* @param Result $result
* @param int $page
* @param int $perPage
* @param array $append
* @param int $page
* @param int $perPage
* @param array $append
*
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
Expand Down Expand Up @@ -446,7 +427,7 @@ public function searchClient()
$this->searchClient = new CloudSearchClient([
'region' => $this->config('config.region'),
'credentials' => $this->config('config.credentials'),
'version' => $this->config('config.version'),
'version' => $this->config('config.version'),
]);
}

Expand All @@ -456,10 +437,10 @@ public function searchClient()
/**
* Create a new query builder instance.
*
* @return StructuredQueryBuilder
* @return Builder
*/
public function newQuery()
{
return new StructuredQueryBuilder;
return new Builder($this);
}
}
6 changes: 4 additions & 2 deletions src/Console/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function processModels($action)
* Get action argument.
*
* @param array $validActions
*
* @return array
*/
protected function getActionArgument($validActions = [])
Expand All @@ -92,8 +93,8 @@ protected function getModelArgument()
{
$models = explode(',', preg_replace('/\s+/', '', $this->argument('model')));

return array_map(function($model) {
$model = array_map(function($m) {
return array_map(function ($model) {
$model = array_map(function ($m) {
return Str::studly($m);
}, explode('\\', $model));

Expand Down Expand Up @@ -143,6 +144,7 @@ protected function setSystemLocale($locale)
* Validate model.
*
* @param string $model
*
* @return bool
*/
protected function validateModel($model)
Expand Down
23 changes: 20 additions & 3 deletions src/Eloquent/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaravelCloudSearch\Eloquent;

use LaravelCloudSearch\Query\Builder;
use LaravelCloudSearch\CloudSearcher;

trait Searchable
Expand Down Expand Up @@ -56,11 +57,27 @@ public function getCloudSearchIndex()
/**
* Perform a search against the model's indexed data.
*
* @return \LaravelCloudSearch\Builder
* @param string $query
*
* @return \LaravelCloudSearch\Query\Builder
*/
public static function search()
public static function search($query)
{
return new Builder(new static);
return self::searchBuilder()->term($query);
}

/**
* Get the search builder instance.
*
* @return \LaravelCloudSearch\Query\Builder
*/
public static function searchBuilder()
{
$instance = new static();

$builder = new Builder($instance->getCloudSearch());

return $builder->searchableType($instance);
}

/**
Expand Down
37 changes: 27 additions & 10 deletions src/Eloquent/Builder.php → src/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
<?php

namespace LaravelCloudSearch\Eloquent;
namespace LaravelCloudSearch\Query;

use Closure;
use LaravelCloudSearch\StructuredQueryBuilder;
use LaravelCloudSearch\CloudSearcher;

class Builder
{
/**
* The model instance.
* Cloud searcher instance.
*
* @var \Illuminate\Database\Eloquent\Model
* @var \LaravelCloudSearch\CloudSearcher
*/
public $model;
public $cloudSearcher;

/**
* Create a new search builder instance.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \LaravelCloudSearch\CloudSearcher $cloudSearcher
*/
public function __construct($model)
public function __construct(CloudSearcher $cloudSearcher)
{
$this->model = $model;
$this->cloudSearcher = $cloudSearcher;
$this->builder = new StructuredQueryBuilder();
}

$this->builder = new StructuredQueryBuilder($this);
/**
* Set the searchable type.
*
* @param mixed $type
*
* @return self
*/
public function searchableType($type)
{
if (is_object($type)) {
$type = get_class($type);
}

$this->phrase($type, 'searchable_type');

return $this;
}

/**
Expand Down Expand Up @@ -529,6 +546,6 @@ public function paginate($perPage = 15)
*/
protected function cloudSearch()
{
return $this->model->getCloudSearch();
return $this->cloudSearcher;
}
}
Loading

0 comments on commit d3da9ff

Please sign in to comment.