Skip to content

Commit

Permalink
Handle types for aggregations in arSolrBoolQuery
Browse files Browse the repository at this point in the history
Add a method that appends types to the aggregations before the query
params are generated.
  • Loading branch information
anvit committed Aug 21, 2024
1 parent 218c2d6 commit 3f99b09
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
8 changes: 2 additions & 6 deletions plugins/arSolrPlugin/lib/arSolrPluginQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
69 changes: 48 additions & 21 deletions plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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) {
Expand All @@ -94,8 +93,8 @@ public function generateQueryParams()
$boolQuery['filter'] = $postFilterQuery;
}

if ($aggregations) {
$boolQuery['facet'] = $aggregations;
if ($facetQuery) {
$boolQuery['facet'] = $facetQuery;
}

if ($sort) {
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 3f99b09

Please sign in to comment.