Skip to content

Commit

Permalink
Merge pull request #6 from matchish/config
Browse files Browse the repository at this point in the history
Add feature "Publish config example"
  • Loading branch information
matchish authored Apr 11, 2019
2 parents bb9fbf6 + 7622030 commit d3ed66f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [1.1.0] - 2019-04-09
### Added
- Default config

## [1.0.0] - 2019-03-30
### Added
- Import console command
- Flush console command
- Implemented all basic scout engine methods
- Implemented all basic scout engine methods
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ For index `products` it will be
`elasticsearch.indices.settigs.products`

And for default settings
`elasticsearch.indices.settigs.default
`
`elasticsearch.indices.settigs.default`

If you need example you can publish default config with artisan command
`php artisan vendor:publish --tag config`
### Zero downtime reimport
While working in production, to keep your existing search experience available while reimporting your data, you also can use `scout:import` Artisan command:

Expand Down Expand Up @@ -115,10 +117,10 @@ $results = Product::search('zonga', function($client, $body) {
})->raw();
```

`$client` is `\ElasticSearch\Client` object from [`elasticsearch/elasticsearch`](https://packagist.org/packages/elasticsearch/elasticsearch) package
`$client` is `\ElasticSearch\Client` object from [elasticsearch/elasticsearch](https://packagist.org/packages/elasticsearch/elasticsearch) package
And `$body` is `ONGR\ElasticsearchDSL\Search` from [ongr/elasticsearch-dsl](https://packagist.org/packages/ongr/elasticsearch-dsl) package

>Don't forget :star: the package if you like it. :pray:
>Don't forget to :star: the package if you like it. :pray:
## :free: License
Scout ElasticSearch is an open-sourced software licensed under the [MIT license](LICENSE.md).
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "This package extends Laravel Scout adding full power of ElasticSearch",
"version": "1.0.0",
"version": "1.1.0",
"keywords": [
"laravel",
"scout",
Expand Down
29 changes: 29 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
'indices' => [
'mappings' => [
'default' => [
'_doc' => [
'properties' => [
'created_at' => [
'type' => 'date',
],
'updated_at' => [
'type' => 'date',
],
'deleted_at' => [
'type' => 'date',
],
],
],
],
],
'settings' => [
'default' => [
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
],
],
];
9 changes: 7 additions & 2 deletions src/ElasticSearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public static function fromSearchable($searchable): Index
$name = $searchable->searchableAs().'_'.time();
$settingsConfigKey = "elasticsearch.indices.settings.{$searchable->searchableAs()}";
$mappingsConfigKey = "elasticsearch.indices.mappings.{$searchable->searchableAs()}";
$settings = config($settingsConfigKey, config('elasticsearch.indices.settings.default'));
$mappings = config($mappingsConfigKey);
$defaultSettings = [
'number_of_shards' => 1,
'number_of_replicas' => 0,

];
$settings = config($settingsConfigKey, config('elasticsearch.indices.settings.default', $defaultSettings));
$mappings = config($mappingsConfigKey, config('elasticsearch.indices.mappings.default'));

return new static($name, $settings, $mappings);
}
Expand Down
9 changes: 4 additions & 5 deletions src/ScoutElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function boot(): void

return new ElasticSearchEngine($elasticsearch);
});

$this->publishes([
__DIR__.'/../config/elasticsearch.php' => config_path('elasticsearch.php'),
], 'config');
}

/**
Expand All @@ -36,11 +40,6 @@ public function register(): void
$this->app->bind(Client::class, function () {
return ClientBuilder::create()->setHosts([env('ELASTICSEARCH_HOST')])->build();
});
config()->set('elasticsearch.indices.settings.default', [
'number_of_shards' => 1,
'number_of_replicas' => 0,

]);

$this->registerCommands();
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Feature/ScoutElasticSearchServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Matchish\ScoutElasticSearch;

use App\Product;
use Tests\TestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;

class ScoutElasticSearchServiceProviderTest extends TestCase
{
public function testConfigPublishing()
{
\File::delete(config_path('elasticsearch.php'));
$provider = new ScoutElasticSearchServiceProvider($this->app);
$provider->boot();

\Artisan::call('vendor:publish', [
'--tag' => 'config',
]);

app('config')->set('elasticsearch', require config_path('elasticsearch.php'));

$this->assertFileExists(config_path('elasticsearch.php'));

$index = Index::fromSearchable(new Product());
$this->assertEquals([
'mappings' => [
'_doc' => [
'properties' => [
'created_at' => [
'type' => 'date',
],
'updated_at' => [
'type' => 'date',
],
'deleted_at' => [
'type' => 'date',
],
],
], ],
'settings' => [
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
], $index->config());
}
}
29 changes: 29 additions & 0 deletions tests/laravel/config/elasticsearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
'indices' => [
'mappings' => [
'default' => [
'_doc' => [
'properties' => [
'created_at' => [
'type' => 'date',
],
'updated_at' => [
'type' => 'date',
],
'deleted_at' => [
'type' => 'date',
],
],
],
],
],
'settings' => [
'default' => [
'number_of_shards' => 1,
'number_of_replicas' => 0,
],
],
],
];

0 comments on commit d3ed66f

Please sign in to comment.