Skip to content

Commit 0b5d8d2

Browse files
committed
2 parents e4f29aa + 3432fae commit 0b5d8d2

File tree

3 files changed

+75
-65
lines changed

3 files changed

+75
-65
lines changed

src/ElasticsearchEngine.php

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace ScoutEngines\Elasticsearch;
44

5-
use Laravel\Scout\Builder;
6-
use Laravel\Scout\Engines\Engine;
75
use Elasticsearch\Client as Elastic;
86
use Illuminate\Database\Eloquent\Collection;
9-
use Illuminate\Support\Collection as BaseCollection;
7+
use Laravel\Scout\Builder;
8+
use Laravel\Scout\Engines\Engine;
109

1110
class ElasticsearchEngine extends Engine
1211
{
@@ -20,7 +19,8 @@ class ElasticsearchEngine extends Engine
2019
/**
2120
* Create a new engine instance.
2221
*
23-
* @param \Elasticsearch\Client $elastic
22+
* @param \Elasticsearch\Client $elastic
23+
*
2424
* @return void
2525
*/
2626
public function __construct(Elastic $elastic)
@@ -31,7 +31,8 @@ public function __construct(Elastic $elastic)
3131
/**
3232
* Update the given model in the index.
3333
*
34-
* @param Collection $models
34+
* @param Collection $models
35+
*
3536
* @return void
3637
*/
3738
public function update($models)
@@ -41,14 +42,14 @@ public function update($models)
4142
$models->each(function ($model) use (&$params) {
4243
$params['body'][] = [
4344
'update' => [
44-
'_id' => $model->getKey(),
45+
'_id' => $model->getKey(),
4546
'_index' => $model->searchableAs(),
46-
'_type' => $model->searchableAs(),
47-
]
47+
'_type' => $model->searchableAs(),
48+
],
4849
];
4950
$params['body'][] = [
50-
'doc' => $model->toSearchableArray(),
51-
'doc_as_upsert' => true
51+
'doc' => $model->toSearchableArray(),
52+
'doc_as_upsert' => true,
5253
];
5354
});
5455

@@ -58,7 +59,8 @@ public function update($models)
5859
/**
5960
* Remove the given model from the index.
6061
*
61-
* @param Collection $models
62+
* @param Collection $models
63+
*
6264
* @return void
6365
*/
6466
public function delete($models)
@@ -68,10 +70,10 @@ public function delete($models)
6870
$models->each(function ($model) use (&$params) {
6971
$params['body'][] = [
7072
'delete' => [
71-
'_id' => $model->getKey(),
73+
'_id' => $model->getKey(),
7274
'_index' => $model->searchableAs(),
73-
'_type' => $model->searchableAs(),
74-
]
75+
'_type' => $model->searchableAs(),
76+
],
7577
];
7678
});
7779

@@ -81,56 +83,59 @@ public function delete($models)
8183
/**
8284
* Perform the given search on the engine.
8385
*
84-
* @param Builder $builder
86+
* @param Builder $builder
87+
*
8588
* @return mixed
8689
*/
8790
public function search(Builder $builder)
8891
{
8992
return $this->performSearch($builder, array_filter([
9093
'numericFilters' => $this->filters($builder),
91-
'size' => $builder->limit,
94+
'size' => $builder->limit,
9295
]));
9396
}
9497

9598
/**
9699
* Perform the given search on the engine.
97100
*
98-
* @param Builder $builder
99-
* @param int $perPage
100-
* @param int $page
101+
* @param Builder $builder
102+
* @param int $perPage
103+
* @param int $page
104+
*
101105
* @return mixed
102106
*/
103107
public function paginate(Builder $builder, $perPage, $page)
104108
{
105109
$result = $this->performSearch($builder, [
106110
'numericFilters' => $this->filters($builder),
107-
'from' => (($page * $perPage) - $perPage),
108-
'size' => $perPage,
111+
'from' => (($page * $perPage) - $perPage),
112+
'size' => $perPage,
109113
]);
110114

111-
$result['nbPages'] = $result['hits']['total']/$perPage;
115+
$result['nbPages'] = $result['hits']['total'] / $perPage;
112116

113117
return $result;
114118
}
115119

116120
/**
117121
* Perform the given search on the engine.
118122
*
119-
* @param Builder $builder
120-
* @param array $options
123+
* @param Builder $builder
124+
* @param array $options
125+
*
121126
* @return mixed
122127
*/
123128
protected function performSearch(Builder $builder, array $options = [])
124129
{
125130
$params = [
126131
'index' => $builder->index ?: $builder->model->searchableAs(),
127-
'body' => [
132+
'body' => [
128133
'query' => [
129134
'bool' => [
130-
'must' => [['query_string' => [ 'query' => "*{$builder->query}*"]]]
131-
]
132-
]
133-
]
135+
'must' => [['query_string' => ['query' => "*{$builder->query}*"]]],
136+
],
137+
],
138+
],
134139
];
135140

136141
if ($sort = $this->sort($builder)) {
@@ -167,7 +172,8 @@ protected function performSearch(Builder $builder, array $options = [])
167172
/**
168173
* Get the filter array for the query.
169174
*
170-
* @param Builder $builder
175+
* @param Builder $builder
176+
*
171177
* @return array
172178
*/
173179
protected function filters(Builder $builder)
@@ -184,7 +190,8 @@ protected function filters(Builder $builder)
184190
/**
185191
* Pluck and return the primary keys of the given results.
186192
*
187-
* @param mixed $results
193+
* @param mixed $results
194+
*
188195
* @return \Illuminate\Support\Collection
189196
*/
190197
public function mapIds($results)
@@ -195,8 +202,9 @@ public function mapIds($results)
195202
/**
196203
* Map the given results to instances of the given model.
197204
*
198-
* @param mixed $results
199-
* @param \Illuminate\Database\Eloquent\Model $model
205+
* @param mixed $results
206+
* @param \Illuminate\Database\Eloquent\Model $model
207+
*
200208
* @return Collection
201209
*/
202210
public function map($results, $model)
@@ -221,7 +229,8 @@ public function map($results, $model)
221229
/**
222230
* Get the total count from a raw result returned by the engine.
223231
*
224-
* @param mixed $results
232+
* @param mixed $results
233+
*
225234
* @return int
226235
*/
227236
public function getTotalCount($results)
@@ -232,13 +241,14 @@ public function getTotalCount($results)
232241
/**
233242
* Generates the sort if theres any.
234243
*
235-
* @param Builder $builder
244+
* @param Builder $builder
245+
*
236246
* @return array|null
237247
*/
238248
protected function sort($builder)
239249
{
240250
if (count($builder->orders) == 0) {
241-
return null;
251+
return;
242252
}
243253

244254
return collect($builder->orders)->map(function ($order) {

src/ElasticsearchProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace ScoutEngines\Elasticsearch;
44

5-
use Laravel\Scout\EngineManager;
6-
use Illuminate\Support\ServiceProvider;
75
use Elasticsearch\ClientBuilder as ElasticBuilder;
6+
use Illuminate\Support\ServiceProvider;
7+
use Laravel\Scout\EngineManager;
88

99
class ElasticsearchProvider extends ServiceProvider
1010
{

tests/ElasticsearchEngineTest.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ public function test_update_adds_objects_to_index()
1717
'body' => [
1818
[
1919
'update' => [
20-
'_id' => 1,
20+
'_id' => 1,
2121
'_index' => 'table',
22-
'_type' => 'table',
23-
]
22+
'_type' => 'table',
23+
],
2424
],
2525
[
26-
'doc' => ['id' => 1 ],
27-
'doc_as_upsert' => true
28-
]
29-
]
26+
'doc' => ['id' => 1],
27+
'doc_as_upsert' => true,
28+
],
29+
],
3030
]);
3131

3232
$engine = new ElasticsearchEngine($client);
33-
$engine->update(Collection::make([new ElasticsearchEngineTestModel]));
33+
$engine->update(Collection::make([new ElasticsearchEngineTestModel()]));
3434
}
3535

3636
public function test_delete_removes_objects_to_index()
@@ -40,41 +40,41 @@ public function test_delete_removes_objects_to_index()
4040
'body' => [
4141
[
4242
'delete' => [
43-
'_id' => 1,
43+
'_id' => 1,
4444
'_index' => 'table',
45-
'_type' => 'table',
46-
]
45+
'_type' => 'table',
46+
],
4747
],
48-
]
48+
],
4949
]);
5050

5151
$engine = new ElasticsearchEngine($client);
52-
$engine->delete(Collection::make([new ElasticsearchEngineTestModel]));
52+
$engine->delete(Collection::make([new ElasticsearchEngineTestModel()]));
5353
}
5454

5555
public function test_search_sends_correct_parameters_to_elasticsearch()
5656
{
5757
$client = Mockery::mock('Elasticsearch\Client');
5858
$client->shouldReceive('search')->with([
5959
'index' => 'table',
60-
'body' => [
60+
'body' => [
6161
'query' => [
6262
'bool' => [
6363
'must' => [
6464
['query_string' => ['query' => '*zonda*']],
6565
['match_phrase' => ['foo' => 1]],
66-
['terms' => ['bar' => [1, 3]]],
67-
]
68-
]
66+
['terms' => ['bar' => [1, 3]]],
67+
],
68+
],
6969
],
7070
'sort' => [
71-
['id' => 'desc']
72-
]
73-
]
71+
['id' => 'desc'],
72+
],
73+
],
7474
]);
7575

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

116116
$results = $engine->map([
117117
'hits' => [
118118
'total' => '1',
119-
'hits' => [
119+
'hits' => [
120120
[
121-
'_id' => '1'
122-
]
123-
]
124-
]
121+
'_id' => '1',
122+
],
123+
],
124+
],
125125
], $model);
126126

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

0 commit comments

Comments
 (0)