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
36 changes: 14 additions & 22 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

class ElasticsearchEngine extends Engine
{
/**
* Index where the models will be saved.
*
* @var string
*/
protected $index;

/**
* Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
*
Expand All @@ -30,10 +23,9 @@ class ElasticsearchEngine extends Engine
* @param \Elasticsearch\Client $elastic
* @return void
*/
public function __construct(Elastic $elastic, $index)
public function __construct(Elastic $elastic)
{
$this->elastic = $elastic;
$this->index = $index;
}

/**
Expand All @@ -46,12 +38,11 @@ public function update($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'update' => [
'_id' => $model->getKey(),
'_index' => $this->index,
'_index' => $model->searchableAs(),
'_type' => $model->searchableAs(),
]
];
Expand All @@ -74,12 +65,11 @@ public function delete($models)
{
$params['body'] = [];

$models->each(function($model) use (&$params)
{
$models->each(function ($model) use (&$params) {
$params['body'][] = [
'delete' => [
'_id' => $model->getKey(),
'_index' => $this->index,
'_index' => $model->searchableAs(),
'_type' => $model->searchableAs(),
]
];
Expand Down Expand Up @@ -118,7 +108,7 @@ public function paginate(Builder $builder, $perPage, $page)
'size' => $perPage,
]);

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

return $result;
}
Expand All @@ -133,8 +123,7 @@ public function paginate(Builder $builder, $perPage, $page)
protected function performSearch(Builder $builder, array $options = [])
{
$params = [
'index' => $this->index,
'type' => $builder->index ?: $builder->model->searchableAs(),
'index' => $builder->index ?: $builder->model->searchableAs(),
'body' => [
'query' => [
'bool' => [
Expand All @@ -157,8 +146,10 @@ protected function performSearch(Builder $builder, array $options = [])
}

if (isset($options['numericFilters']) && count($options['numericFilters'])) {
$params['body']['query']['bool']['must'] = array_merge($params['body']['query']['bool']['must'],
$options['numericFilters']);
$params['body']['query']['bool']['must'] = array_merge(
$params['body']['query']['bool']['must'],
$options['numericFilters']
);
}

if ($builder->callback) {
Expand Down Expand Up @@ -218,7 +209,8 @@ public function map($results, $model)
->pluck('_id')->values()->all();

$models = $model->whereIn(
$model->getKeyName(), $keys
$model->getKeyName(),
$keys
)->get()->keyBy($model->getKeyName());

return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
Expand Down Expand Up @@ -249,7 +241,7 @@ protected function sort($builder)
return null;
}

return collect($builder->orders)->map(function($order) {
return collect($builder->orders)->map(function ($order) {
return [$order['column'] => $order['direction']];
})->toArray();
}
Expand Down
8 changes: 4 additions & 4 deletions src/ElasticsearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class ElasticsearchProvider extends ServiceProvider
*/
public function boot()
{
app(EngineManager::class)->extend('elasticsearch', function($app) {
return new ElasticsearchEngine(ElasticBuilder::create()
app(EngineManager::class)->extend('elasticsearch', function ($app) {
return new ElasticsearchEngine(
ElasticBuilder::create()
->setHosts(config('scout.elasticsearch.hosts'))
->build(),
config('scout.elasticsearch.index')
->build()
);
});
}
Expand Down
13 changes: 6 additions & 7 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_update_adds_objects_to_index()
[
'update' => [
'_id' => 1,
'_index' => 'scout',
'_index' => 'table',
'_type' => 'table',
]
],
Expand All @@ -29,7 +29,7 @@ public function test_update_adds_objects_to_index()
]
]);

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

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

$engine = new ElasticsearchEngine($client, 'scout');
$engine = new ElasticsearchEngine($client);
$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' => 'scout',
'type' => 'table',
'index' => 'table',
'body' => [
'query' => [
'bool' => [
Expand All @@ -74,7 +73,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
]
]);

$engine = new ElasticsearchEngine($client, 'scout');
$engine = new ElasticsearchEngine($client);
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
$builder->where('foo', 1);
$builder->where('bar', [1, 3]);
Expand Down