Skip to content

Commit 6b9c654

Browse files
committed
Preparation for the removal of mapping types from elasticsearch.
Removal of 'index' configuration, separate indexes per eloquent model type.
1 parent bee74a3 commit 6b9c654

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
44
[![Build Status](https://travis-ci.com/boxed-code/laravel-scout-elasticsearch.svg?branch=master)](https://travis-ci.com/boxed-code/laravel-scout-elasticsearch)
55

6-
This is a basic [elastic](https://www.elastic.co/products/elasticsearch) search backed driver [for Laravel Scout](https://laravel.com/docs/5.8/scout).
6+
A basic [elastic](https://www.elastic.co/products/elasticsearch) search backed driver [for Laravel Scout](https://laravel.com/docs/5.8/scout).
77

88
This driver has a simple configuration, requiring you to only publish the configuration and set the hostname of your elasticsearch node.
99

10-
Based on the original work of [ErickTamayo](https://github.com/ErickTamayo/laravel-scout-elastic).
10+
Note that this driver uses a separate index for each model type as elasticsearch mapping types have been deprecated in elasticsearch 6.0 and will be removed in 8.0, for more information see https://bit.ly/2TZVZvq.
1111

12-
**Requires Scout 5.*, 6.* or 7.*, Laravel >=5.4, PHP >=7.0**
12+
Based on the original work of [ErickTamayo](https://github.com/ErickTamayo/laravel-scout-elastic). **Requires Scout ^5.0, ^6.0, or ^7.0, Laravel >=5.4, PHP >=7.0**
1313

1414
## Contents
1515

src/ElasticsearchEngine.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@
44

55
use Laravel\Scout\Builder;
66
use Laravel\Scout\Engines\Engine;
7-
use Elasticsearch\Client as Elastic;
7+
use Elasticsearch\Client;
88
use Illuminate\Database\Eloquent\Collection;
99

1010
class ElasticsearchEngine extends Engine
11-
{
12-
/**
13-
* Index where the models will be saved.
14-
*
15-
* @var string
16-
*/
17-
protected $index;
18-
11+
{
1912
/**
2013
* Elastic where the instance of Elastic|\Elasticsearch\Client is stored.
2114
*
@@ -29,10 +22,9 @@ class ElasticsearchEngine extends Engine
2922
* @param \Elasticsearch\Client $elastic
3023
* @return void
3124
*/
32-
public function __construct(Elastic $elastic, $index)
25+
public function __construct(Client $elastic)
3326
{
3427
$this->elastic = $elastic;
35-
$this->index = $index;
3628
}
3729

3830
/**
@@ -50,7 +42,12 @@ public function update($models)
5042
$params['body'][] = [
5143
'update' => [
5244
'_id' => $model->getKey(),
53-
'_index' => $this->index,
45+
'_index' => $model->searchableAs(),
46+
/**
47+
* @deprecated Document mapping types scheduled deprecated in
48+
* elasticsearch 6.0 will be removed in 8.0.
49+
* https://bit.ly/2TZVZvq
50+
*/
5451
'_type' => $model->searchableAs(),
5552
]
5653
];
@@ -78,7 +75,12 @@ public function delete($models)
7875
$params['body'][] = [
7976
'delete' => [
8077
'_id' => $model->getKey(),
81-
'_index' => $this->index,
78+
'_index' => $model->searchableAs(),
79+
/**
80+
* @deprecated Document mapping types scheduled deprecated in
81+
* elasticsearch 6.0 will be removed in 8.0.
82+
* https://bit.ly/2TZVZvq
83+
*/
8284
'_type' => $model->searchableAs(),
8385
]
8486
];
@@ -132,8 +134,13 @@ public function paginate(Builder $builder, $perPage, $page)
132134
protected function performSearch(Builder $builder, array $options = [])
133135
{
134136
$params = [
135-
'index' => $this->index,
136-
'type' => $builder->index ?: $builder->model->searchableAs(),
137+
'index' => $builder->model->searchableAs(),
138+
/**
139+
* @deprecated Document mapping types scheduled deprecated in
140+
* elasticsearch 6.0 will be removed in 8.0.
141+
* https://bit.ly/2TZVZvq
142+
*/
143+
'type' => $builder->model->searchableAs(),
137144
'body' => [
138145
'query' => [
139146
'bool' => [

src/ElasticsearchServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class ElasticsearchServiceProvider extends ServiceProvider
1414
public function boot()
1515
{
1616
app(EngineManager::class)->extend('elasticsearch', function($app) {
17-
return new ElasticsearchEngine(ElasticBuilder::create()
17+
$client = ElasticBuilder::create()
1818
->setHosts(config('scout.elasticsearch.hosts'))
19-
->build(),
20-
config('scout.elasticsearch.index')
21-
);
19+
->build();
20+
21+
return new ElasticsearchEngine($client);
2222
});
2323
}
2424
}

tests/ElasticsearchEngineTest.php

Lines changed: 8 additions & 8 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 TestModel]));
3434
}
3535

@@ -41,22 +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 TestModel]));
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',
59+
'index' => 'table',
6060
'type' => 'table',
6161
'body' => [
6262
'query' => [
@@ -74,7 +74,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
7474
]
7575
]);
7676

77-
$engine = new ElasticsearchEngine($client, 'scout');
77+
$engine = new ElasticsearchEngine($client);
7878
$builder = new \Laravel\Scout\Builder(new TestModel, 'zonda');
7979
$builder->where('foo', 1);
8080
$builder->where('bar', [1, 3]);
@@ -88,7 +88,7 @@ public function test_builder_callback_can_manipulate_search_parameters_to_elasti
8888
$client = Mockery::mock(\Elasticsearch\Client::class);
8989
$client->shouldReceive('search')->with('modified_by_callback');
9090

91-
$engine = new ElasticsearchEngine($client, 'scout');
91+
$engine = new ElasticsearchEngine($client);
9292
$builder = new \Laravel\Scout\Builder(
9393
new TestModel(),
9494
'huayra',
@@ -107,7 +107,7 @@ function (\Elasticsearch\Client $client, $query, $params) {
107107
public function test_map_correctly_maps_results_to_models()
108108
{
109109
$client = Mockery::mock('Elasticsearch\Client');
110-
$engine = new ElasticsearchEngine($client, 'scout');
110+
$engine = new ElasticsearchEngine($client);
111111

112112
$builder = Mockery::mock(Builder::class);
113113

0 commit comments

Comments
 (0)