Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: elegisandi/aws-elasticsearch-laravel
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.4.4
Choose a base ref
...
head repository: elegisandi/aws-elasticsearch-laravel
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 5 commits
  • 8 files changed
  • 1 contributor

Commits on Jul 20, 2018

  1. enable support for elasticsearch client methods

    Elegi Sandi committed Jul 20, 2018
    Copy the full SHA
    d558e73 View commit details

Commits on Sep 18, 2018

  1. added default time filter field; updated README

    Elegi Sandi committed Sep 18, 2018
    Copy the full SHA
    57fb396 View commit details

Commits on Sep 25, 2018

  1. set count method return to integer

    Elegi Sandi committed Sep 25, 2018
    Copy the full SHA
    6a9d9c6 View commit details
  2. added update index mapping console command

    Elegi Sandi committed Sep 25, 2018
    Copy the full SHA
    e224ab6 View commit details

Commits on Sep 26, 2018

  1. added multiple range fields in search; added short and long data type…

    …s to keyword type props in search query filters
    Elegi Sandi committed Sep 26, 2018
    Copy the full SHA
    3b4b394 View commit details
85 changes: 56 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ AWS Elasticsearch Service for Laravel/Lumen

- Add facade to your `config/app.php` aliases

'ElasticSearch' => elegisandi\AWSElasticsearchService\ElasticSearchFacade::class
'ElasticSearch' => elegisandi\AWSElasticsearchService\Facades\ElasticSearch::class
- Set AWS credentials and Elasticsearch config in your `.env` file

@@ -29,6 +29,7 @@ AWS Elasticsearch Service for Laravel/Lumen
ELASTICSEARCH_REPLICAS
ELASTICSEARCH_DEFAULT_INDEX
ELASTICSEARCH_DEFAULT_TYPE
ELASTICSEARCH_DEFAULT_TIME_FILTER_FIELD

When you are already using aws elasticsearch service, set

@@ -94,121 +95,147 @@ AWS Elasticsearch Service for Laravel/Lumen

`php artisan elasticsearch:create-index --reset`

* Update Index Mapping _(updates the default index mapping)_

`php artisan elasticsearch:update-index-mapping`

> Only supports new properties updates.
## Available Methods

* ##### aggregations(array $aggs, array $query = [], array $options = [], $type, $index)
* ##### aggregations(`array $aggs`, `array $query = []`, `array $options = []`, `$type`, `$index`)

> **$aggs** : must follow the structure specified in [elasticsearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html).
> **$query** : see `search` method `$query` argument
> **$query** : see **`search`** method **`$query`** argument
> **$options** : see `search` method `$options` argument
> **$options** : see **`search`** method **`$options`** argument
> returns `Array`
* ##### search(array $query = [], array $options = [], array $range = [], $type, $index)
* ##### search(`array $query = []`, `array $options = []`, `array $range = []`, `$type`, `$index`)

> **$query** : an array of key-value pair of any available properties
> **$options** : an array of key-value pair of these params: _(from, size, sort)_
> **$options** : an array of key-value pair of the ff: `from`, `size`, `sort`
> **$range** : an array representation of [range query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html).
> returns `Array`
* count(array $query = [], array $range = [], $type, $index)
* ##### count(`array $query = []`, `array $range = []`, `$type`, `$index`)

> a _(syntactic sugar)_ method of search with zero hits result
> returns `Array`
> returns `Int`
* ##### setSearchParams(`Request $request`, `array $defaults = []`, `$type`)

> an optional and conventional approach of setting search params via query string
> **$request** : an instance of `\Illuminate\Http\Request`, query variables in used:
* setSearchParams(Request $request, array $defaults = [], $type)
- `range`, see [getDateRange](https://github.com/elegisandi/aws-elasticsearch-laravel#getDateRange-range-format-null) method
- `start`, a valid date string
- `end`, a valid date string
- `sort`, a mapping property
- `order`, value is either `desc` or `asc`
- `size`, total results to return _(max of 10000)_

> **$defaults** : an array of key-value pair of the ff: `sort, order, size`
> returns `Array`
* getDateRange($range, $format = null)
* ##### getDateRange(`$range`, `$format = null`)

> **$range** : predefined date range values: `today, yesterday, last-7-days, this-month, last-month, last-2-months, last-3-months`
> **$format** must be a valid date format, default is `null` which will return a DateTime instance
> returns `Array`
* setAggregationDailyDateRanges($start, $end, $format = null)
* ##### setAggregationDailyDateRanges(`$start`, `$end`, `$format = null`)

> **$format** must be a valid date format, default is `null` which will return a DateTime instance
> returns `Array`
* defaultAggregationNames
* ##### defaultAggregationNames

> returns `Array`
* defaultIndex
* ##### defaultIndex

> returns `String`
* defaultType
* ##### defaultType

> returns `String`
* ##### defaultTimeFilterField

> returns `String`
* setSearchQueryFilters(Collection $query, array $bool_clauses = [], $type = null)
* ##### setSearchQueryFilters(`Collection $query`, `array $bool_clauses = []`, `$type = null`)

> returns `Array`
* setBoolQueryClause(Collection $query, array $properties, $context, $occur, callable $callback = null)
* ##### setBoolQueryClause(`Collection $query`, `array $properties`, `$context`, `$occur`, `callable $callback = null`)

> returns `Array`
* getMappingPropertiesByDataType(Collection $properties, $data_type)
* ##### getMappingPropertiesByDataType(`Collection $properties`, `$data_type`)

> returns `Array`
* getMappingProperties($type = null)
* ##### getMappingProperties(`$type = null`)

> returns `Collection`
* indexDocument(array $body, $type = null, $index = null)
* ##### indexDocument(`array $body`, `$type = null`, `$index = null`)

> returns `Array`
* getDocument($id, $type, $index)
* ##### getDocument(`$id`, `$type`, `$index`)

> returns `Array`
* updateDocument(array $fields, $id, $type = null, $index = null)
* ##### updateDocument(`array $fields`, `$id`, `$type = null`, `$index = null`)

> returns `Array`
* deleteDocument($id, $type = null, $index = null)
* ##### deleteDocument(`$id`, `$type = null`, `$index = null`)

> returns `Array`
* getSettings($index = null)
* ##### getSettings(`$index = null`)

> returns `Array`
* updateSettings(array $settings, $index)
* ##### updateSettings(`array $settings`, `$index`)

> returns `Array`
* getMappings($index, $type)
* ##### getMappings(`$index, $type`)

> returns `Array`
* updateMappings(array $properties, $type, $index)
* ##### updateMappings(`array $properties`, `$type`, `$index`)

> returns `Array`
* createIndex(array $mappings, array $settings, $index)
* ##### createIndex(`array $mappings`, `array $settings`, `$index`)

* getIndex($index = null)
* ##### getIndex($index = null)

> returns `Boolean`
* deleteIndex($index)
* ##### deleteIndex(`$index`)

> returns `Array`
### NOTE: All methods of the elasticsearch client are now supported.

## Limitations

- Supported data types in **search** method are:
1 change: 1 addition & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
'defaults' => [
'index' => env('ELASTICSEARCH_DEFAULT_INDEX'),
'type' => env('ELASTICSEARCH_DEFAULT_TYPE'),
'time_filter_field' => env('ELASTICSEARCH_DEFAULT_TIME_FILTER_FIELD'),
'aggregation_names' => [],
],
];
43 changes: 18 additions & 25 deletions src/Commands/CreateIndex.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;
use elegisandi\AWSElasticsearchService\Facades\ElasticSearch;
use Exception;

/**
@@ -35,37 +36,29 @@ class CreateIndex extends Command
public function handle()
{
if ($this->laravel instanceof LaravelApplication) {
try {
$action = 'created';

if ($reset = $this->option('reset')) {
\elegisandi\AWSElasticsearchService\ElasticSearchFacade::deleteIndex();

$action = 'reset';
}

\elegisandi\AWSElasticsearchService\ElasticSearchFacade::createIndex();

$this->info('Elasticsearch index has been successfully ' . $action . '.');
} catch (Exception $e) {
$this->error($e->getMessage());
}
$class = ElasticSearch::class;
} elseif ($this->laravel instanceof LumenApplication) {
try {
$action = 'created';
$class = app('elasticsearch');
}

if ($this->option('reset')) {
app('elasticsearch')->deleteIndex();
if (empty($class)) {
$this->error('Application not supported.');
}

$action = 'reset';
}
try {
$action = 'created';

app('elasticsearch')->createIndex();
if ($this->option('reset')) {
call_user_func([$class, 'deleteIndex']);

$this->info('Elasticsearch index has been successfully ' . $action . '.');
} catch (Exception $e) {
$this->error($e->getMessage());
$action = 'reset';
}

call_user_func([$class, 'createIndex']);

$this->info('Elasticsearch index has been successfully ' . $action . '.');
} catch (Exception $e) {
$this->error($e->getMessage());
}
}
}
68 changes: 68 additions & 0 deletions src/Commands/UpdateIndexMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace elegisandi\AWSElasticsearchService\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;
use elegisandi\AWSElasticsearchService\Facades\ElasticSearch;
use Exception;

/**
* Class UpdateIndexMapping
* @package elegisandi\AWSElasticsearchService\Commands
*/
class UpdateIndexMapping extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'elasticsearch:update-index-mapping';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update current index mapping.';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
if ($this->laravel instanceof LaravelApplication) {
$class = ElasticSearch::class;
} elseif ($this->laravel instanceof LumenApplication) {
$class = app('elasticsearch');
}

if (empty($class)) {
$this->error('Application not supported.');
}

try {
$index = call_user_func([$class, 'defaultIndex']);
$type = call_user_func([$class, 'defaultType']);
$mapping = call_user_func([$class, 'getMapping']);
$properties = $mapping[$index]['mappings'][$type]['properties'];
$config_props = call_user_func([$class, 'getMappingProperties'])->toArray();

$new_props = array_diff_key($config_props, $properties);

if (empty($new_props)) {
$this->warn('Nothing to update for the current index mapping.');
}

call_user_func([$class, 'updateMapping'], $new_props);

$this->info('Elasticsearch index mapping has been successfully updated.');
} catch (Exception $e) {
$this->error($e->getMessage());
}
}
}
16 changes: 10 additions & 6 deletions src/ElasticSearch.php
Original file line number Diff line number Diff line change
@@ -98,7 +98,9 @@ private function search(array $query = [], array $options = [], array $range = [

// set date range if not empty
if (!empty($range)) {
$filters['must'][] = ['range' => $range];
foreach ($range as $field => $value) {
$filters['must'][] = ['range' => [$field => $value]];
}
}

// set bool query if filters not empty
@@ -132,12 +134,14 @@ private function search(array $query = [], array $options = [], array $range = [
* @param array $range
* @param string $type
* @param string $index
* @return array|null
* @return int
* @throws \Exception
*/
private function count(array $query = [], array $range = [], $type, $index)
{
return $this->search($query, ['size' => 0], $range, $type, $index);
$hits = $this->search($query, ['size' => 0], $range, $type, $index);

return $hits['count'] ?? 0;
}

/**
@@ -228,7 +232,7 @@ private function updateSettings(array $settings, $index)
* @param string $type
* @return array
*/
private function getMappings($index, $type)
private function getMapping($index, $type)
{
$params = array_filter(compact('index', 'type'));

@@ -241,7 +245,7 @@ private function getMappings($index, $type)
* @param string $index
* @return array
*/
private function updateMappings(array $properties, $type, $index)
private function updateMapping(array $properties, $type, $index)
{
// Set the index and type
$params = [
@@ -309,7 +313,7 @@ protected function buildClient()

if ($this->config['aws']) {
$psr7Handler = \Aws\default_http_handler();
$signer = new \Aws\Signature\SignatureV4('es', config('aws.region'));
$signer = new \Aws\Signature\SignatureV4('es', config('aws.region', env('AWS_REGION')));
$credentialProvider = \Aws\Credentials\CredentialProvider::defaultProvider();

$handler = function (array $request) use ($psr7Handler, $signer, $credentialProvider) {
1 change: 1 addition & 0 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->commands([
\elegisandi\AWSElasticsearchService\Commands\CreateIndex::class,
\elegisandi\AWSElasticsearchService\Commands\UpdateIndexMapping::class,
]);
}

Loading