Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Post Meta and Generic Term Aggregations #56

Merged
merged 8 commits into from
Oct 17, 2023
Prev Previous commit
Next Next commit
Add ability to register a generic term aggregation
  • Loading branch information
kevinfodness committed Oct 6, 2023
commit ea0fa1f78a1d9271b1fef112e0e28c580d7f787a
23 changes: 23 additions & 0 deletions lib/adapters/class-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Elasticsearch_Extensions\Aggregations\Post_Type;
use Elasticsearch_Extensions\Aggregations\Relative_Date;
use Elasticsearch_Extensions\Aggregations\Taxonomy;
use Elasticsearch_Extensions\Aggregations\Term;
use Elasticsearch_Extensions\DSL;
use Elasticsearch_Extensions\Interfaces\Hookable;

Expand Down Expand Up @@ -171,6 +172,28 @@ public function add_taxonomy_aggregation( string $taxonomy, array $args = [] ):
$this->add_aggregation( new Taxonomy( $this->dsl, wp_parse_args( $args, [ 'taxonomy' => $taxonomy ] ) ) );
}

/**
* Adds a new generic term aggregation to the list of active aggregations.
*
* @param string $term_field The term field to aggregate on.
* @param string $query_var The query var to use for this aggregation for filters on the front-end.
* @param array $args Arguments to pass to the adapter's aggregation configuration.
*/
public function add_term_aggregation( string $term_field, string $query_var, array $args = [] ): void {
$this->add_aggregation(
new Term(
$this->dsl,
wp_parse_args(
$args,
[
'query_var' => $query_var,
'term_field' => $this->dsl->map_field( $term_field ),
]
)
)
);
}

/**
* Get an aggregation by its label.
*
Expand Down
18 changes: 18 additions & 0 deletions lib/class-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ public function enable_taxonomy_aggregation( string $taxonomy, array $args = []
return $this;
}

/**
* A function to enable a generic Elasticsearch 'term' aggregation. Users must provide an
* Elasticsearch term field to aggregate on and a query var to use. This function should only
* be used if a more specific term-type aggregation (e.g., taxonomy, post type) is not
* available for the kind of aggregation you want to create.
*
* @param string $term_field The term field to aggregate on.
* @param string $query_var The query var to use for this aggregation for filters on the front-end.
* @param array $args Arguments to pass to the adapter's aggregation configuration.
*
* @return Controller The instance of the class to allow for chaining.
*/
public function enable_term_aggregation( string $term_field, string $query_var, array $args = [] ): Controller {
$this->adapter->add_term_aggregation( $term_field, $query_var, $args );

return $this;
}

/**
* Get a specific aggregation from the adapter by its label.
*
Expand Down
Loading