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 aggregations back to the query #3847

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ public function query( $index, $type, $query, $query_args, $query_object = null
* @param {array} $query_args Current WP Query arguments
*/
do_action( 'ep_retrieve_aggregations', $response['aggregations'], $query, '', $query_args );

if ( is_object( $query_object ) ) {
if ( method_exists( $query_object, 'set' ) ) {
$query_object->set( 'ep_aggregations', $response['aggregations'] );
} else {
$query_object->query_vars['ep_aggregations'] = $response['aggregations'];
}
}
}

/**
Expand Down Expand Up @@ -456,11 +464,15 @@ public function query( $index, $type, $query, $query_args, $query_object = null
*
* @hook ep_es_query_results
* @param {array} $results Results from Elasticsearch
* @param {response} $response Raw response from Elasticsearch
* @param {array} $query Raw Elasticsearch query
* @param {array} $query_args Query arguments
* @param {mixed} $query_object Could be WP_Query, WP_User_Query, etc.
* @return {array} New results
* @param {int} $results.found_documents Total number of documents.
* @param {array} $results.documents Array of documents.
* @param {array} $results.aggregations Array of aggregations.
* @param {array} $results.suggest Array of suggestions.
* @param {response} $response Raw response from Elasticsearch
* @param {array} $query Raw Elasticsearch query
* @param {array} $query_args Query arguments
* @param {mixed} $query_object Could be WP_Query, WP_User_Query, etc.
* @return {array} New results
*/
return apply_filters(
'ep_es_query_results',
Expand Down
29 changes: 29 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -9146,4 +9146,33 @@ public function test_mapping_ep_stop_filter() {
$index_settings = $settings[ $index_name ]['settings'];
$this->assertSame( '_arabic_', $index_settings['index.analysis.filter.ep_stop.stopwords'] );
}

/**
* Test if aggregations are set
*
* @since 5.1.0
* @group post
*/
public function test_aggregations_return() {
$query = new \WP_Query(
[
'ep_integrate' => true,
'fields' => 'ids',
'aggs' => [
'name' => 'my_aggs',
'aggs' => [
'terms' => [
'size' => 10000,
'field' => 'terms.category.slug',
],
],
],
'ep_custom_id' => 'my_query',
]
);

$this->assertTrue( $query->elasticsearch_success );
$this->assertArrayHasKey( 'ep_aggregations', $query->query_vars );
$this->assertArrayHasKey( 'my_aggs', $query->query_vars['ep_aggregations'] );
}
}
Loading