diff --git a/README.md b/README.md index 120bdd5..fc39710 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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` diff --git a/src/ElasticSearch.php b/src/ElasticSearch.php index 0f8d9e4..3c8a0b2 100644 --- a/src/ElasticSearch.php +++ b/src/ElasticSearch.php @@ -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, @@ -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 { @@ -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)) { diff --git a/src/ElasticSearchFacade.php b/src/ElasticSearchFacade.php index fa8ae31..a9e0b49 100644 --- a/src/ElasticSearchFacade.php +++ b/src/ElasticSearchFacade.php @@ -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) @@ -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) diff --git a/src/Traits/ElasticSearchHelper.php b/src/Traits/ElasticSearchHelper.php index 41db1bd..fc88983 100644 --- a/src/Traits/ElasticSearchHelper.php +++ b/src/Traits/ElasticSearchHelper.php @@ -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; }