diff --git a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php index 5705b09cb3..af3846e81a 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php @@ -19,6 +19,13 @@ class arSolrBoolQuery extends arSolrAbstractQuery { + /** + * Field type. + * + * @var string + */ + protected ?string $type = null; + /** * Assemble BoolQuery. * @@ -29,10 +36,12 @@ public function generateQueryParams() $params = $this->getParams(); $aggregations = $this->getAggregations(); $sort = $this->getSort(); + $type = $this->getType(); $mustQuery = []; $mustNotQuery = []; $shouldQuery = []; + $postFilterQuery = []; $boolQuery = [ 'query' => [], @@ -58,6 +67,17 @@ public function generateQueryParams() array_push($shouldQuery, $shouldClause); } + foreach ($params['post_filter'] as $query) { + $postFilterParams = $query->getQueryParams(); + $postFilterClause = $postFilterParams['query']; + array_push($postFilterQuery, $postFilterClause); + } + + // TODO: handle setting types for aggregations + if ($aggregations && !$type) { + throw new Exception("Field 'type' must be set if using aggregations."); + } + if ($shouldQuery) { $boolQuery['query']['bool']['should'] = $shouldQuery; } @@ -70,6 +90,10 @@ public function generateQueryParams() $boolQuery['query']['bool']['must_not'] = $mustNotQuery; } + if ($postFilterQuery) { + $boolQuery['filter'] = $postFilterQuery; + } + if ($aggregations) { $boolQuery['facet'] = $aggregations; } @@ -98,6 +122,18 @@ 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); + } + public function setSort($sort) { foreach ($sort as $field => $direction) { @@ -171,6 +207,34 @@ public function setFrom($offset) return $this->setOffset($offset); } + public function setType($type) + { + $params = $this->getParams(); + + foreach ($params['must'] as $query) { + $this->_setTypeForQuery($query, $type); + } + + foreach ($params['must_not'] as $query) { + $this->_setTypeForQuery($query, $type); + } + + foreach ($params['should'] as $query) { + $this->_setTypeForQuery($query, $type); + } + + foreach ($params['post_filter'] as $query) { + $this->_setTypeForQuery($query, $type); + } + + $this->type = $type; + } + + public function getType() + { + return $this->type; + } + protected function _addQuery(string $type, $args): self { if (!\is_array($args) && !($args instanceof arSolrAbstractQuery)) { @@ -179,4 +243,21 @@ protected function _addQuery(string $type, $args): self return $this->addParam($type, $args); } + + private function _setTypeForQuery($query, $type) + { + if (!($query instanceof arSolrAbstractQuery)) { + throw new Exception('Invalid Query. Has to be array or instance of arSolrAbstractQuery'); + } + + // MatchAll queries have no type since they're run on all fields + if ($query instanceof arSolrMatchAllQuery) { + return; + } + + // Only set the type for another arSolrBoolQuery or if it is not already set + if (!$query->getType()) { + $query->setType($type); + } + } } diff --git a/plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php index 273b9a930c..4bada8dabf 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php @@ -24,18 +24,21 @@ class arSolrIdsQuery extends arSolrTermsQuery * * @param mixed $searchQuery * @param null|mixed $term + * @param null|mixed $ids */ public function __construct($ids = null) { $this->setIds($ids); } - public function setIds($ids) { + public function setIds($ids) + { $this->termField = 'id'; $this->termValues = $ids; } - public function getIds() { + public function getIds() + { return $this->termValues; } @@ -43,7 +46,7 @@ protected function generateQueryParams() { $termField = $this->getTermField(); $ids = $this->getIds(); - if (!isset($ids) || count($ids) == 0) { + if (!isset($ids) || 0 == count($ids)) { throw new Exception('Ids are not set.'); } @@ -52,7 +55,7 @@ protected function generateQueryParams() throw new Exception("Field 'type' is not set."); } - $queryString = implode(" OR ", $ids); + $queryString = implode(' OR ', $ids); $this->query = [ 'query' => [ 'edismax' => [ diff --git a/plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php b/plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php index b3faf789c9..281b5373df 100644 --- a/plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php +++ b/plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php @@ -116,7 +116,7 @@ protected function generateQueryParams() throw new Exception("Field 'type' is not set."); } - $queryString = implode(" OR ", $termValues); + $queryString = implode(' OR ', $termValues); $this->query = [ 'query' => [ 'edismax' => [ diff --git a/test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php b/test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php index da8c15928c..697d415957 100644 --- a/test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php +++ b/test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php @@ -157,5 +157,4 @@ public function testGetQueryParamsException($ids, $type, $expectedException, $ex $this->idsQuery->getQueryParams(); } - }