Skip to content

Commit 13933d3

Browse files
authored
Merge pull request #2 from sl0wik/feature/multiple_indices
Multiple indices support
2 parents 199fa96 + 504ac51 commit 13933d3

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

src/ElasticsearchEngine.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@
1010

1111
class ElasticsearchEngine extends Engine
1212
{
13-
/**
14-
* Index where the models will be saved.
15-
*
16-
* @var string
17-
*/
18-
protected $index;
19-
2013
/**
2114
* Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
2215
*
@@ -30,10 +23,9 @@ class ElasticsearchEngine extends Engine
3023
* @param \Elasticsearch\Client $elastic
3124
* @return void
3225
*/
33-
public function __construct(Elastic $elastic, $index)
26+
public function __construct(Elastic $elastic)
3427
{
3528
$this->elastic = $elastic;
36-
$this->index = $index;
3729
}
3830

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

49-
$models->each(function($model) use (&$params)
50-
{
41+
$models->each(function ($model) use (&$params) {
5142
$params['body'][] = [
5243
'update' => [
5344
'_id' => $model->getKey(),
54-
'_index' => $this->index,
45+
'_index' => $model->searchableAs(),
5546
'_type' => $model->searchableAs(),
5647
]
5748
];
@@ -74,12 +65,11 @@ public function delete($models)
7465
{
7566
$params['body'] = [];
7667

77-
$models->each(function($model) use (&$params)
78-
{
68+
$models->each(function ($model) use (&$params) {
7969
$params['body'][] = [
8070
'delete' => [
8171
'_id' => $model->getKey(),
82-
'_index' => $this->index,
72+
'_index' => $model->searchableAs(),
8373
'_type' => $model->searchableAs(),
8474
]
8575
];
@@ -118,7 +108,7 @@ public function paginate(Builder $builder, $perPage, $page)
118108
'size' => $perPage,
119109
]);
120110

121-
$result['nbPages'] = $result['hits']['total']/$perPage;
111+
$result['nbPages'] = $result['hits']['total']/$perPage;
122112

123113
return $result;
124114
}
@@ -133,8 +123,7 @@ public function paginate(Builder $builder, $perPage, $page)
133123
protected function performSearch(Builder $builder, array $options = [])
134124
{
135125
$params = [
136-
'index' => $this->index,
137-
'type' => $builder->index ?: $builder->model->searchableAs(),
126+
'index' => $builder->index ?: $builder->model->searchableAs(),
138127
'body' => [
139128
'query' => [
140129
'bool' => [
@@ -157,8 +146,10 @@ protected function performSearch(Builder $builder, array $options = [])
157146
}
158147

159148
if (isset($options['numericFilters']) && count($options['numericFilters'])) {
160-
$params['body']['query']['bool']['must'] = array_merge($params['body']['query']['bool']['must'],
161-
$options['numericFilters']);
149+
$params['body']['query']['bool']['must'] = array_merge(
150+
$params['body']['query']['bool']['must'],
151+
$options['numericFilters']
152+
);
162153
}
163154

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

220211
$models = $model->whereIn(
221-
$model->getKeyName(), $keys
212+
$model->getKeyName(),
213+
$keys
222214
)->get()->keyBy($model->getKeyName());
223215

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

252-
return collect($builder->orders)->map(function($order) {
244+
return collect($builder->orders)->map(function ($order) {
253245
return [$order['column'] => $order['direction']];
254246
})->toArray();
255247
}

src/ElasticsearchProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class ElasticsearchProvider extends ServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
app(EngineManager::class)->extend('elasticsearch', function($app) {
17-
return new ElasticsearchEngine(ElasticBuilder::create()
16+
app(EngineManager::class)->extend('elasticsearch', function ($app) {
17+
return new ElasticsearchEngine(
18+
ElasticBuilder::create()
1819
->setHosts(config('scout.elasticsearch.hosts'))
19-
->build(),
20-
config('scout.elasticsearch.index')
20+
->build()
2121
);
2222
});
2323
}

tests/ElasticsearchEngineTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function test_update_adds_objects_to_index()
1818
[
1919
'update' => [
2020
'_id' => 1,
21-
'_index' => 'scout',
21+
'_index' => 'table',
2222
'_type' => 'table',
2323
]
2424
],
@@ -29,7 +29,7 @@ public function test_update_adds_objects_to_index()
2929
]
3030
]);
3131

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

@@ -41,23 +41,22 @@ public function test_delete_removes_objects_to_index()
4141
[
4242
'delete' => [
4343
'_id' => 1,
44-
'_index' => 'scout',
44+
'_index' => 'table',
4545
'_type' => 'table',
4646
]
4747
],
4848
]
4949
]);
5050

51-
$engine = new ElasticsearchEngine($client, 'scout');
51+
$engine = new ElasticsearchEngine($client);
5252
$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([
59-
'index' => 'scout',
60-
'type' => 'table',
59+
'index' => 'table',
6160
'body' => [
6261
'query' => [
6362
'bool' => [
@@ -74,7 +73,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
7473
]
7574
]);
7675

77-
$engine = new ElasticsearchEngine($client, 'scout');
76+
$engine = new ElasticsearchEngine($client);
7877
$builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda');
7978
$builder->where('foo', 1);
8079
$builder->where('bar', [1, 3]);

0 commit comments

Comments
 (0)