Skip to content

Commit

Permalink
added query option to aggregations; added bool clauses array as 2nd a…
Browse files Browse the repository at this point in the history
…rg to set search query filters method
  • Loading branch information
Elegi Sandi committed Apr 2, 2018
1 parent 09440b9 commit faf43f1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ AWS Elasticsearch Service for Laravel/Lumen

## Available Methods

* ##### aggregations(array $aggs, $type, $index)
* ##### aggregations(array $aggs, array $query = [], $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
> returns `Array`
* ##### search(array $query = [], array $options = [], array $range = [], $type, $index)
Expand Down Expand Up @@ -143,7 +145,7 @@ AWS Elasticsearch Service for Laravel/Lumen

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

> returns `Array`
Expand Down
18 changes: 13 additions & 5 deletions src/ElasticSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ public function __construct()

/**
* @param array $aggs
* @param array $query
* @param string $type
* @param string $index
* @return array|null
* @throws \Exception
*/
private function aggregations(array $aggs, $type, $index)
private function aggregations(array $aggs, array $query = [], $type, $index)
{
$params = [
'index' => $index,
Expand All @@ -51,6 +52,16 @@ private function aggregations(array $aggs, $type, $index)
],
];

// create query filters
$filters = $this->setSearchQueryFilters(collect($query), [], $type);

// set bool query if filters not empty
if (!empty($filters)) {
$params['body']['query'] = [
'bool' => $filters
];
}

$aggregations = null;

try {
Expand Down Expand Up @@ -81,11 +92,8 @@ private function search(array $query = [], array $options = [], array $range = [
'body' => $options,
];

// convert query to collection for easier manipulation
$query = collect($query);

// create query filters
$filters = $this->setSearchQueryFilters($query, $type);
$filters = $this->setSearchQueryFilters(collect($query), [], $type);

// set date range if not empty
if (!empty($range)) {
Expand Down
4 changes: 2 additions & 2 deletions src/ElasticSearchFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Class ElasticSearchFacade
* @package elegisandi\AWSElasticsearchService
*
* @method static array aggregations(array $aggs, $type = null, $index = null)
* @method static array aggregations(array $aggs, array $query = [], $type = null, $index = null)
* @method static array search(array $query = [], array $options = [], array $range = [], $type = null, $index = null)
* @method static array count(array $query = [], array $range = [], $type = null, $index = null)
* @method static array setSearchParams(Request $request, array $defaults = [], $type = null)
Expand All @@ -19,7 +19,7 @@
* @method static array defaultAggregationNames
* @method static string defaultIndex
* @method static string defaultType
* @method static array setSearchQueryFilters(Collection $query, $type = null)
* @method static array setSearchQueryFilters(Collection $query, array $bool_clauses = [], $type = null)
* @method static array setBoolQueryClause(Collection $query, array $properties, $context, $occur, callable $callback = null)
* @method static array getMappingPropertiesByDataType(Collection $properties, $data_type)
* @method static Collection getMappingProperties($type = null)
Expand Down
46 changes: 23 additions & 23 deletions src/Traits/ElasticSearchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,43 +228,43 @@ protected function defaultType()

/**
* @param Collection $query
* @param array $bool_clauses
* @param string $type
* @return array
*/
protected function setSearchQueryFilters(Collection $query, $type)
protected function setSearchQueryFilters(Collection $query, array $bool_clauses = [], $type)
{
$filters = [];

if ($query->isEmpty()) {
return $filters;
}
if ($query->isNotEmpty()) {

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

// get text type properties
$text_type_props = $this->getMappingPropertiesByDataType($properties, 'text');
// get text type properties
$text_type_props = $this->getMappingPropertiesByDataType($properties, 'text');

// get keyword type properties
// included types: keyword, ip, integer, array
$keyword_type_props = $this->getMappingPropertiesByDataType($properties, ['keyword', 'ip', 'integer', 'array']);
// get keyword type properties
// included types: keyword, ip, integer, array
$keyword_type_props = $this->getMappingPropertiesByDataType($properties, ['keyword', 'ip', 'integer', 'array']);

// get boolean type properties
$bool_type_props = $this->getMappingPropertiesByDataType($properties, 'boolean');
// get boolean type properties
$bool_type_props = $this->getMappingPropertiesByDataType($properties, 'boolean');

// prepare keyword data type filter
$term_filter = $this->setBoolQueryClause($query, $keyword_type_props, 'term', 'filter');
// prepare keyword data type filter
$bool_clauses[] = $this->setBoolQueryClause($query, $keyword_type_props, 'term', 'filter');

// prepare text data type matching
$full_text_match = $this->setBoolQueryClause($query, $text_type_props, 'match', 'must');
// prepare text data type matching
$bool_clauses[] = $this->setBoolQueryClause($query, $text_type_props, 'match', 'must');

// prepare boolean data type filter
$bool_filter = $this->setBoolQueryClause($query, $bool_type_props, 'term', 'filter', function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
});
// prepare boolean data type filter
$bool_clauses[] = $this->setBoolQueryClause($query, $bool_type_props, 'term', 'filter', function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
});
}

foreach (compact('term_filter', 'full_text_match', 'bool_filter') as $filter) {
foreach ($filter as $occur => $context) {
foreach ($bool_clauses as $clause) {
foreach ($clause as $occur => $context) {
foreach ($context as $field) {
$filters[$occur][] = $field;
}
Expand Down

0 comments on commit faf43f1

Please sign in to comment.