Skip to content

Commit

Permalink
added default time filter field; updated README
Browse files Browse the repository at this point in the history
Elegi Sandi committed Sep 18, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d558e73 commit 57fb396
Showing 6 changed files with 75 additions and 43 deletions.
76 changes: 48 additions & 28 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

@@ -97,115 +98,134 @@ AWS Elasticsearch Service for Laravel/Lumen

## 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`
* setSearchParams(Request $request, array $defaults = [], $type)
* ##### 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:
- `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`
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' => [],
],
];
4 changes: 2 additions & 2 deletions src/Commands/CreateIndex.php
Original file line number Diff line number Diff line change
@@ -39,12 +39,12 @@ public function handle()
$action = 'created';

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

$action = 'reset';
}

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

$this->info('Elasticsearch index has been successfully ' . $action . '.');
} catch (Exception $e) {
2 changes: 1 addition & 1 deletion src/ElasticSearch.php
Original file line number Diff line number Diff line change
@@ -309,7 +309,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) {
8 changes: 4 additions & 4 deletions src/ElasticSearchFacade.php → src/Facades/ElasticSearch.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace elegisandi\AWSElasticsearchService;
namespace elegisandi\AWSElasticsearchService\Facades;

use Illuminate\Support\Facades\Facade;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

/**
* Class ElasticSearchFacade
* @package elegisandi\AWSElasticsearchService
* Class ElasticSearch
* @package elegisandi\AWSElasticsearchService\Facades
*
* @method static array aggregations(array $aggs, array $query = [], array $options = [], $type = null, $index = null)
* @method static array search(array $query = [], array $options = [], array $range = [], $type = null, $index = null)
@@ -35,7 +35,7 @@
* @method static bool getIndex($index = null)
* @method static array deleteIndex($index = null)
*/
class ElasticSearchFacade extends Facade
class ElasticSearch extends Facade
{
/**
* Get the registered name of the component.
27 changes: 19 additions & 8 deletions src/Traits/ElasticSearchHelper.php
Original file line number Diff line number Diff line change
@@ -40,9 +40,12 @@ private function setSearchParams(Request $request, array $defaults = [], $type)
extract($this->getDateRange($range));
}

// get default time filter field
$time_filter_field = $this->defaultTimeFilterField();

// set default values
$defaults = array_merge([
'sort' => 'timestamp',
'sort' => $time_filter_field,
'order' => 'desc',
'size' => 30
], $defaults);
@@ -55,7 +58,7 @@ private function setSearchParams(Request $request, array $defaults = [], $type)
// parse and set start date if valid
if (!empty($start)) {
try {
$date_range['timestamp']['gte'] = Carbon::parse($start)->startOfDay()->toDateTimeString();
$date_range[$time_filter_field]['gte'] = Carbon::parse($start)->startOfDay()->toDateTimeString();
} catch (Exception $e) {
$invalid_start = true;
$start = Carbon::now()->subWeek()->toDateString();
@@ -65,17 +68,17 @@ private function setSearchParams(Request $request, array $defaults = [], $type)
// parse and set end date if valid
if (!empty($end)) {
try {
$date_range['timestamp']['lte'] = Carbon::parse($end)->endOfDay()->toDateTimeString();
$date_range[$time_filter_field]['lte'] = Carbon::parse($end)->endOfDay()->toDateTimeString();
} catch (Exception $e) {
$invalid_end = true;
$end = Carbon::yesterday()->toDateString();
}
}

// set timestamp format and timezone if any
if (!empty($date_range['timestamp'])) {
$date_range['timestamp']['format'] = 'yyyy-MM-dd HH:mm:ss';
$date_range['timestamp']['time_zone'] = config('app.timezone');
// set time filter field format and timezone if any
if (!empty($date_range[$time_filter_field])) {
$date_range[$time_filter_field]['format'] = 'yyyy-MM-dd HH:mm:ss';
$date_range[$time_filter_field]['time_zone'] = config('app.timezone');
}

// validate sort
@@ -260,6 +263,14 @@ protected function defaultType()
return $this->config['defaults']['type'];
}

/**
* @return string
*/
protected function defaultTimeFilterField()
{
return $this->config['defaults']['time_filter_field'];
}

/**
* @param Collection $query
* @param array $bool_clauses
@@ -270,7 +281,7 @@ protected function setSearchQueryFilters(Collection $query, array $bool_clauses
{
$filters = [];

if ($query->isNotEmpty()) {
if (!$query->isEmpty()) {

// get properties
$properties = $this->getMappingProperties($type);

0 comments on commit 57fb396

Please sign in to comment.