10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 312 forks source link

How to change aggregation scope - simple question - hopefully quick soln #3825

Closed gosixl closed 9 months ago

gosixl commented 9 months ago

Describe your question

Hi guys, after a wp_query, I print the global ep_facet_aggs and get aggregates for the entire data set.

How can i change the scope of the aggregation to be just that of the items i queried? e.g. 10 posts containing "Hello" rather than all of my posts.

Code of Conduct

gosixl commented 9 months ago

Sorry to be specific, this seems to work fine if we do an "s" parametered WP_Query. However aggregates don't seem to be forthcoming for where "ep_integrate" is used without a search.

felipeelia commented 9 months ago

Hi @gosixl. If you are doing that manually, you'll need to apply the same filters to the aggregation as well. Here is how we do that in our Filters feature (previously called "Facets"): https://github.com/10up/ElasticPress/blob/develop/includes/classes/Feature/Facets/Facets.php#L153

This is the part where we calculate the filters applied to the query and reproduce that in the aggregations:

        remove_filter( 'ep_post_formatted_args', [ $this, 'set_agg_filters' ], 10, 3 );
        $facet_formatted_args = Indexables::factory()->get( 'post' )->format_args( $query_args, $query );
        add_filter( 'ep_post_formatted_args', [ $this, 'set_agg_filters' ], 10, 3 );