Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 46 additions & 36 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace ScoutEngines\Elasticsearch;

use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use Elasticsearch\Client as Elastic;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as BaseCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;

class ElasticsearchEngine extends Engine
{
Expand All @@ -20,7 +19,8 @@ class ElasticsearchEngine extends Engine
/**
* Create a new engine instance.
*
* @param \Elasticsearch\Client $elastic
* @param \Elasticsearch\Client $elastic
*
* @return void
*/
public function __construct(Elastic $elastic)
Expand All @@ -31,7 +31,8 @@ public function __construct(Elastic $elastic)
/**
* Update the given model in the index.
*
* @param Collection $models
* @param Collection $models
*
* @return void
*/
public function update($models)
Expand All @@ -41,14 +42,14 @@ public function update($models)
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
'_id' => $model->getKey(),
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
'_type' => $model->searchableAs(),
]
'_type' => $model->searchableAs(),
],
];
$params['body'][] = [
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true
'doc' => $model->toSearchableArray(),
'doc_as_upsert' => true,
];
});

Expand All @@ -58,7 +59,8 @@ public function update($models)
/**
* Remove the given model from the index.
*
* @param Collection $models
* @param Collection $models
*
* @return void
*/
public function delete($models)
Expand All @@ -68,10 +70,10 @@ public function delete($models)
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'delete' => [
'_id' => $model->getKey(),
'_id' => $model->getKey(),
'_index' => $model->searchableAs(),
'_type' => $model->searchableAs(),
]
'_type' => $model->searchableAs(),
],
];
});

Expand All @@ -81,56 +83,59 @@ public function delete($models)
/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param Builder $builder
*
* @return mixed
*/
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'numericFilters' => $this->filters($builder),
'size' => $builder->limit,
'size' => $builder->limit,
]));
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param int $perPage
* @param int $page
* @param Builder $builder
* @param int $perPage
* @param int $page
*
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
{
$result = $this->performSearch($builder, [
'numericFilters' => $this->filters($builder),
'from' => (($page * $perPage) - $perPage),
'size' => $perPage,
'from' => (($page * $perPage) - $perPage),
'size' => $perPage,
]);

$result['nbPages'] = $result['hits']['total']/$perPage;
$result['nbPages'] = $result['hits']['total'] / $perPage;

return $result;
}

/**
* Perform the given search on the engine.
*
* @param Builder $builder
* @param array $options
* @param Builder $builder
* @param array $options
*
* @return mixed
*/
protected function performSearch(Builder $builder, array $options = [])
{
$params = [
'index' => $builder->index ?: $builder->model->searchableAs(),
'body' => [
'body' => [
'query' => [
'bool' => [
'must' => [['query_string' => [ 'query' => "*{$builder->query}*"]]]
]
]
]
'must' => [['query_string' => ['query' => "*{$builder->query}*"]]],
],
],
],
];

if ($sort = $this->sort($builder)) {
Expand Down Expand Up @@ -167,7 +172,8 @@ protected function performSearch(Builder $builder, array $options = [])
/**
* Get the filter array for the query.
*
* @param Builder $builder
* @param Builder $builder
*
* @return array
*/
protected function filters(Builder $builder)
Expand All @@ -184,7 +190,8 @@ protected function filters(Builder $builder)
/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @param mixed $results
*
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
Expand All @@ -195,8 +202,9 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return Collection
*/
public function map($results, $model)
Expand All @@ -221,7 +229,8 @@ public function map($results, $model)
/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @param mixed $results
*
* @return int
*/
public function getTotalCount($results)
Expand All @@ -232,13 +241,14 @@ public function getTotalCount($results)
/**
* Generates the sort if theres any.
*
* @param Builder $builder
* @param Builder $builder
*
* @return array|null
*/
protected function sort($builder)
{
if (count($builder->orders) == 0) {
return null;
return;
}

return collect($builder->orders)->map(function ($order) {
Expand Down
4 changes: 2 additions & 2 deletions src/ElasticsearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ScoutEngines\Elasticsearch;

use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Elasticsearch\ClientBuilder as ElasticBuilder;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;

class ElasticsearchProvider extends ServiceProvider
{
Expand Down
54 changes: 27 additions & 27 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public function test_update_adds_objects_to_index()
'body' => [
[
'update' => [
'_id' => 1,
'_id' => 1,
'_index' => 'table',
'_type' => 'table',
]
'_type' => 'table',
],
],
[
'doc' => ['id' => 1 ],
'doc_as_upsert' => true
]
]
'doc' => ['id' => 1],
'doc_as_upsert' => true,
],
],
]);

$engine = new ElasticsearchEngine($client);
$engine->update(Collection::make([new ElasticsearchEngineTestModel]));
$engine->update(Collection::make([new ElasticsearchEngineTestModel()]));
}

public function test_delete_removes_objects_to_index()
Expand All @@ -40,41 +40,41 @@ public function test_delete_removes_objects_to_index()
'body' => [
[
'delete' => [
'_id' => 1,
'_id' => 1,
'_index' => 'table',
'_type' => 'table',
]
'_type' => 'table',
],
],
]
],
]);

$engine = new ElasticsearchEngine($client);
$engine->delete(Collection::make([new ElasticsearchEngineTestModel]));
$engine->delete(Collection::make([new ElasticsearchEngineTestModel()]));
}

public function test_search_sends_correct_parameters_to_elasticsearch()
{
$client = Mockery::mock('Elasticsearch\Client');
$client->shouldReceive('search')->with([
'index' => 'table',
'body' => [
'body' => [
'query' => [
'bool' => [
'must' => [
['query_string' => ['query' => '*zonda*']],
['match_phrase' => ['foo' => 1]],
['terms' => ['bar' => [1, 3]]],
]
]
['terms' => ['bar' => [1, 3]]],
],
],
],
'sort' => [
['id' => 'desc']
]
]
['id' => 'desc'],
],
],
]);

$engine = new ElasticsearchEngine($client);
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel(), 'zonda');
$builder->where('foo', 1);
$builder->where('bar', [1, 3]);
$builder->orderBy('id', 'desc');
Expand Down Expand Up @@ -111,17 +111,17 @@ public function test_map_correctly_maps_results_to_models()
$model = Mockery::mock('Illuminate\Database\Eloquent\Model');
$model->shouldReceive('getKeyName')->andReturn('id');
$model->shouldReceive('whereIn')->once()->with('id', ['1'])->andReturn($model);
$model->shouldReceive('get')->once()->andReturn(Collection::make([new ElasticsearchEngineTestModel]));
$model->shouldReceive('get')->once()->andReturn(Collection::make([new ElasticsearchEngineTestModel()]));

$results = $engine->map([
'hits' => [
'total' => '1',
'hits' => [
'hits' => [
[
'_id' => '1'
]
]
]
'_id' => '1',
],
],
],
], $model);

$this->assertEquals(1, count($results));
Expand Down