Skip to content

Commit

Permalink
Add setType, setPostFilter to arSolrBoolQuery
Browse files Browse the repository at this point in the history
Add a method to arSolrBoolQuery that sets the types for its child
queries. Also add a method for setting filters for bool queries.
  • Loading branch information
anvit committed Aug 20, 2024
1 parent 91fafa5 commit 98143dc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
81 changes: 81 additions & 0 deletions plugins/arSolrPlugin/lib/query/arSolrBoolQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

class arSolrBoolQuery extends arSolrAbstractQuery
{
/**
* Field type.
*
* @var string
*/
protected ?string $type = null;

/**
* Assemble BoolQuery.
*
Expand All @@ -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' => [],
Expand All @@ -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;
}
Expand All @@ -70,6 +90,10 @@ public function generateQueryParams()
$boolQuery['query']['bool']['must_not'] = $mustNotQuery;
}

if ($postFilterQuery) {
$boolQuery['filter'] = $postFilterQuery;
}

if ($aggregations) {
$boolQuery['facet'] = $aggregations;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}
}
11 changes: 7 additions & 4 deletions plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@ 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;
}

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.');
}

Expand All @@ -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' => [
Expand Down
2 changes: 1 addition & 1 deletion plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
1 change: 0 additions & 1 deletion test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,4 @@ public function testGetQueryParamsException($ids, $type, $expectedException, $ex

$this->idsQuery->getQueryParams();
}

}

0 comments on commit 98143dc

Please sign in to comment.