From 3f99b09507c7eb0feb3d338c917d8ccd24593d6b Mon Sep 17 00:00:00 2001 From: Anvit Srivastav Date: Wed, 21 Aug 2024 15:35:15 -0700 Subject: [PATCH] Handle types for aggregations in arSolrBoolQuery Add a method that appends types to the aggregations before the query params are generated. --- .../lib/arSolrPluginQuery.class.php | 8 +-- .../lib/query/arSolrBoolQuery.class.php | 69 +++++++++++++------ 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/plugins/arSolrPlugin/lib/arSolrPluginQuery.class.php b/plugins/arSolrPlugin/lib/arSolrPluginQuery.class.php index 54d46ec4e3..e23294ee65 100644 --- a/plugins/arSolrPlugin/lib/arSolrPluginQuery.class.php +++ b/plugins/arSolrPlugin/lib/arSolrPluginQuery.class.php @@ -213,9 +213,7 @@ public function addAdvancedSearchFilters( ) ); - /** - * TODO: Remove ES nested query - */ + // TODO: Remove ES nested query // // Use nested query and mapping object to allow querying // // over the actor and event ids from the same event // $queryNested = new arSolrNestedQuery(); @@ -670,9 +668,7 @@ protected function getDateRangeQuery($params) $query->addMust(new arSolrRangeQuery('dates.endDate', $range)); } - /** - * TODO: Remove ES nested query - */ + // TODO: Remove ES nested query // // Use nested query and mapping object to allow querying // // over the start and end dates from the same event // $queryNested = new arSolrNestedQuery(); diff --git a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php index af3846e81a..c879527e5d 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php @@ -34,13 +34,12 @@ class arSolrBoolQuery extends arSolrAbstractQuery public function generateQueryParams() { $params = $this->getParams(); - $aggregations = $this->getAggregations(); $sort = $this->getSort(); - $type = $this->getType(); $mustQuery = []; $mustNotQuery = []; $shouldQuery = []; + $facetQuery = []; $postFilterQuery = []; $boolQuery = [ @@ -73,9 +72,9 @@ public function generateQueryParams() array_push($postFilterQuery, $postFilterClause); } - // TODO: handle setting types for aggregations - if ($aggregations && !$type) { - throw new Exception("Field 'type' must be set if using aggregations."); + foreach ($params['aggregations'] as $name => $agg) { + $facet = $this->_generateFacetFromAggregation($name, $agg); + array_push($facetQuery, $facet); } if ($shouldQuery) { @@ -94,8 +93,8 @@ public function generateQueryParams() $boolQuery['filter'] = $postFilterQuery; } - if ($aggregations) { - $boolQuery['facet'] = $aggregations; + if ($facetQuery) { + $boolQuery['facet'] = $facetQuery; } if ($sort) { @@ -110,25 +109,11 @@ public function generateQueryParams() $this->query = $boolQuery; } - public function getAggregations() - { - $params = $this->getParams(); - - return $params['aggregations']; - } - public function setAggregations($agg) { return $this->addParam('aggregations', $agg); } - public function getPostFilter() - { - $params = $this->getParams(); - - return $params['post_filter']; - } - public function setPostFilter($filter) { return $this->addParam('post_filter', $filter); @@ -244,6 +229,48 @@ protected function _addQuery(string $type, $args): self return $this->addParam($type, $args); } + private function _generateFacetFromAggregation($name, $agg) + { + $type = $this->getType(); + if (!$type) { + throw new Exception("Field 'type' must be set if using aggregations."); + } + + $facet = [ + $name => [ + 'type' => $agg['type'], + ], + ]; + + if (isset($agg['limit'])) { + $facet[$name]['limit'] = $agg['limit']; + } + + switch ($agg['type']) { + case 'terms': + $facet = [ + $name => [ + 'field' => "{$type}.{$agg['field']}", + ], + ]; + + break; + + case 'query': + foreach ($agg['field'] as $field => $value) { + $facet = [ + $name => [ + 'q' => "{$type}.{$field}:{$value}", + ], + ]; + } + + break; + } + + return $facet; + } + private function _setTypeForQuery($query, $type) { if (!($query instanceof arSolrAbstractQuery)) {