Skip to content

Commit

Permalink
Merge pull request #59 from devgeniem/groupby-filter
Browse files Browse the repository at this point in the history
Groupby filter
  • Loading branch information
villesiltala authored Mar 2, 2020
2 parents abe5ea5 + 0cb5b83 commit e6a15d6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,20 @@ public function search( \WP_Query $query ) {
$offset = 0;
}

// Get the sortby parameter
// Get query parameters
$sortby = $this->query_builder->get_sortby() ?: [];
$applies = $this->query_builder->get_applies() ?: [];
$filters = $this->query_builder->get_filters() ?: [];
$reduce_functions = $this->query_builder->get_reduce_functions() ?: [];
$groupby = $this->query_builder->get_groupby();

// Filters for query parts
$sortby = apply_filters( 'redipress/sortby', $sortby );
$applies = apply_filters( 'redipress/applies', $applies );
$filters = apply_filters( 'redipress/filters', $filters );
$groupby = apply_filters( 'redipress/groupby', $groupby );
$reduce_functions = apply_filters( 'redipress/reduce_functions', $reduce_functions );
$load = apply_filters( 'redipress/load', [ 'post_object' ] );

// Form the return field clause
$return_fields = array_map( function( string $field ) use ( $reduce_functions ) : array {
Expand All @@ -152,8 +161,8 @@ public function search( \WP_Query $query ) {
$command = array_merge(
[ $this->index, $search_query_string, 'INFIELDS', count( $infields ) ],
$infields,
[ 'LOAD', 1, '@post_object' ],
$this->query_builder->get_groupby(),
array_merge( [ 'LOAD', count( $load ) ], array_map( function( $l ) { return '@' . $l; }, $load ) ),
array_merge( [ 'GROUPBY', count( $groupby ) ], array_map( function( $g ) { return '@' . $g; }, $groupby ) ),
array_reduce( $return_fields, 'array_merge', [] ),
$applies,
$filters,
Expand Down
4 changes: 2 additions & 2 deletions src/Search/PostQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PostQueryBuilder extends QueryBuilder {
*
* @var array
*/
protected $groupby = [ '@post_id' ];
protected $groupby = [ 'post_id' ];

/**
* Return fields
Expand Down Expand Up @@ -781,7 +781,7 @@ protected function get_orderby() : bool {
[ 'SORTBY', ( count( $sortby ) * 2 ) ],
array_reduce( $sortby, function( $carry, $item ) {

// Store groupby these need to be in sync with sortby params.
// Store to return fields array, these need to be in sync with sortby params.
$this->return_fields[] = $item['orderby'];

return array_merge( $carry, [ '@' . $item['orderby'], $item['order'] ] );
Expand Down
23 changes: 18 additions & 5 deletions src/Search/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public function get_sortby() : array {
return $this->sortby;
}

/**
* Return the possible apply clauses.
*
* @return array
*/
public function get_applies() : array {
return [];
}

/**
* Return the possible filters
*
* @return array
*/
public function get_filters() : array {
return [];
}

/**
* Return the return fields
*
Expand Down Expand Up @@ -447,11 +465,6 @@ protected function orderby() : string {
* @return string
*/
public function get_groupby() : array {

// Add groupby count as a first item in the array.
// example [ 2, @post_id, @post_date ].
array_unshift( $this->groupby, 'GROUPBY', count( $this->groupby ) );

return $this->groupby;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Search/UserQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UserQueryBuilder extends QueryBuilder {
*
* @var array
*/
protected $groupby = [ '@user_id' ];
protected $groupby = [ 'user_id' ];

/**
* Return fields
Expand Down Expand Up @@ -611,7 +611,7 @@ protected function get_orderby() : bool {
array_reduce( $sortby, function( $carry, $item ) {

// Store groupby these need to be in sync with sortby params.
$this->groupby[] = '@' . $item['orderby'];
$this->groupby[] = $item['orderby'];

return array_merge( $carry, [ '@' . $item['orderby'], $item['order'] ] );
}, [] )
Expand Down
6 changes: 5 additions & 1 deletion src/UserQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public function search( $query ) {
$query->request = 'FT.SEARCH ' . implode( ' ', $command );
}
else {
$groupby = $this->query_builder->get_groupby();

$groupby = apply_filters( 'redipress/user_groupby', $groupby );

$return_fields = array_map( function( string $field ) use ( $reduce_functions ) : array {
$return = [
'REDUCE',
Expand All @@ -190,7 +194,7 @@ public function search( $query ) {
[ $this->index, $search_query_string, 'INFIELDS', count( $infields ) ],
$infields,
[ 'LOAD', 1, '@user_object' ],
$this->query_builder->get_groupby(),
array_merge( [ 'GROUPBY', count( $groupby ) ], array_map( function( $g ) { return '@' . $g; }, $groupby ) ),
array_reduce( $return_fields, 'array_merge', [] ),
array_merge( $sortby ),
[ 'LIMIT', $offset, $limit ]
Expand Down

0 comments on commit e6a15d6

Please sign in to comment.