Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge d7e52b6 into f315318
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 17, 2019
2 parents f315318 + d7e52b6 commit 37cc529
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 222 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -320,8 +320,12 @@ This library supports specifying a prefix for index names, similarly to how many
order to allow multiple applications to share the same cache. This means you can use a single Elasticsearch cluster
for multiple projects (or for example a shared one for the "dev" and "staging" environment).

The prefix used is specified by the `ELASTICSEARCH_INDEX_PREFIX` environment variable. If you have an index named
`content` and you specify `foo` as a prefix, the index will be named `foo_content`.
The prefix used is specified by the configuration file (`config/elasticsearch.php`). The default behavior is to read the
prefix from the `ELASTICSEARCH_INDEX_PREFIX` environment variable.

If you have an index named
`content` and you specify `foo` as a prefix, the index will be named `foo_content`. If you need custom logic you
can override `getPrefixedIndexName()` in `ElasticsearchService`.

Prefixing is supported for index migrations too, in which case the both the indices and the aliases created are
prefixed.
Expand Down
6 changes: 5 additions & 1 deletion config/elasticsearch.php
Expand Up @@ -10,6 +10,10 @@
|
*/

'hosts' => ['localhost:9200'],
'hosts' => ['localhost:9200'],

/*
* The prefix to use for index names
*/
'index_prefix' => env('ELASTICSEARCH_INDEX_PREFIX'),
];
6 changes: 3 additions & 3 deletions src/Console/ApplyMigrationCommand.php
Expand Up @@ -4,7 +4,6 @@

use League\Pipeline\Pipeline;
use Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException;
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage;
Expand Down Expand Up @@ -59,8 +58,9 @@ public function handle()
try {
$pipeline->process($payload);

$this->output->writeln(sprintf('Migrated %s to %s', $payload->getPrefixedIndexName(),
$payload->getPrefixedTargetVersionName()));
$this->output->writeln(sprintf('Migrated %s to %s',
$this->elasticsearchService->getPrefixedIndexName($payload->getIndexName()),
$this->elasticsearchService->getPrefixedIndexName($payload->getTargetVersionName())));
} catch (IndexExistsException $e) {
$this->output->writeln('No migration required');
}
Expand Down
4 changes: 1 addition & 3 deletions src/Console/CreateCommand.php
@@ -1,7 +1,5 @@
<?php namespace Nord\Lumen\Elasticsearch\Console;

use Nord\Lumen\Elasticsearch\IndexNamePrefixer;

class CreateCommand extends AbstractCommand
{

Expand Down Expand Up @@ -35,7 +33,7 @@ public function handle()
}

$params = require($filePath);
$params = IndexNamePrefixer::getPrefixedIndexParameters($params);
$params = $this->elasticsearchService->getPrefixedIndexParameters($params);

$this->info(sprintf("Creating index '%s' ...", $params['index']));

Expand Down
4 changes: 1 addition & 3 deletions src/Console/DeleteCommand.php
@@ -1,7 +1,5 @@
<?php namespace Nord\Lumen\Elasticsearch\Console;

use Nord\Lumen\Elasticsearch\IndexNamePrefixer;

class DeleteCommand extends AbstractCommand
{

Expand All @@ -24,7 +22,7 @@ class DeleteCommand extends AbstractCommand
*/
public function handle()
{
$index = IndexNamePrefixer::getPrefixedIndexName((string)$this->argument('index'));
$index = $this->elasticsearchService->getPrefixedIndexName((string)$this->argument('index'));

$this->info(sprintf("Deleting index '%s' ...", $index));

Expand Down
5 changes: 2 additions & 3 deletions src/Console/IndexCommand.php
Expand Up @@ -3,7 +3,6 @@
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkAction;
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkQuery;
use Nord\Lumen\Elasticsearch\Documents\Bulk\BulkResponseAggregator;
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;

abstract class IndexCommand extends AbstractCommand
{
Expand Down Expand Up @@ -62,8 +61,8 @@ public function handle()
*/
protected function indexData(string $indexName): void
{
$indexName = IndexNamePrefixer::getPrefixedIndexName($indexName);
$indexName = $this->elasticsearchService->getPrefixedIndexName($indexName);

$this->info(sprintf('Indexing data of type "%s" into "%s"', $this->getType(), $indexName));

$data = $this->getData();
Expand Down
25 changes: 2 additions & 23 deletions src/Console/UpdateIndexSettingsCommand.php
Expand Up @@ -2,15 +2,11 @@

namespace Nord\Lumen\Elasticsearch\Console;

use Illuminate\Console\Command;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\IndexNamePrefixer;

/**
* Class SetIndexSettingsCommand
* @package namespace Nord\Lumen\Elasticsearch\Console
*/
class UpdateIndexSettingsCommand extends Command
class UpdateIndexSettingsCommand extends AbstractCommand
{

/**
Expand All @@ -26,26 +22,9 @@ class UpdateIndexSettingsCommand extends Command
*/
protected $description = 'Updates specified dynamic index settings for the specified index';

/**
* @var ElasticsearchServiceContract
*/
private $elasticsearchService;

/**
* SetIndexSettingsCommand constructor.
*
* @param ElasticsearchServiceContract $elasticsearchService
*/
public function __construct(ElasticsearchServiceContract $elasticsearchService)
{
parent::__construct();

$this->elasticsearchService = $elasticsearchService;
}

public function handle(): void
{
$index = IndexNamePrefixer::getPrefixedIndexName($this->input->getArgument('index'));
$index = $this->elasticsearchService->getPrefixedIndexName($this->input->getArgument('index'));
$numReplicas = $this->input->getOption('numReplicas');
$refreshInterval = $this->input->getOption('refreshInterval');

Expand Down
28 changes: 15 additions & 13 deletions src/Contracts/ElasticsearchServiceContract.php
Expand Up @@ -15,97 +15,85 @@ interface ElasticsearchServiceContract
*/
public function search(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function index(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function reindex(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function updateByQuery(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function bulk(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function delete(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function deleteByQuery(array $params = []);


/**
* @param array $params
*
* @return array
*/
public function create(array $params = []);


/**
* @param array $params
*
* @return array|bool
*/
public function exists(array $params = []);


/**
* @return TasksNamespace
*/
public function tasks();


/**
* @return IndicesNamespace
*/
public function indices();


/**
* @return Search
*/
public function createSearch();


/**
* @return Sort
*/
public function createSort();


/**
* @param Search $search
*
* @return array
*/
public function execute(Search $search);
Expand All @@ -116,4 +104,18 @@ public function execute(Search $search);
* @return int
*/
public function count(Search $search): int;

/**
* @param string $indexName
*
* @return string
*/
public function getPrefixedIndexName(string $indexName): string;

/**
* @param array $parameters
*
* @return array
*/
public function getPrefixedIndexParameters(array $parameters): array;
}

0 comments on commit 37cc529

Please sign in to comment.