Skip to content

Commit

Permalink
Avoid storing too much data in the transient
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Aug 24, 2022
1 parent a57c7c5 commit 2626fb4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
40 changes: 38 additions & 2 deletions includes/classes/Feature/Facets/Types/Meta/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function set_wp_query_aggs( $facet_aggs ) {
*
* @since 4.3.0
* @hook ep_facet_meta_size
* @param {int} $size The number of different values. Default: 1000
* @param {int} $size The number of different values. Default: 10000
* @param {string} $field The meta field
* @return {string} The new number of different values
*/
Expand Down Expand Up @@ -289,7 +289,43 @@ public function get_meta_values( string $meta_key ) : array {

$meta_values = get_transient( self::TRANSIENT_PREFIX . $meta_key );
if ( ! $meta_values ) {
$meta_values = \ElasticPress\Indexables::factory()->get( 'post' )->get_all_distinct_values( "meta.{$meta_key}.raw" );
$meta_values = \ElasticPress\Indexables::factory()->get( 'post' )->get_all_distinct_values( "meta.{$meta_key}.raw", 100 );

/**
* Max length of each value in the facet.
*
* To set it to only display 3 characters of each value when the meta_key is `my_key`:
* ```
* add_filter(
* 'ep_facet_meta_value_max_strlen',
* function( $length, $meta_key ) {
* if ( 'my_key' !== $meta_key ) {
* return $length;
* }
* return 3;
* },
* 10,
* 3
* );
* ```
*
* Please note that this value is cached. After adding that code to your codebase you will need
* to clear WordPress's cache or save a post.
*
* @since 4.3.0
* @hook ep_facet_meta_value_max_strlen
* @param {int} $length Length of each value. Defaults to 100.
* @param {string} $meta_key Key of the field.
* @return {int} New length.
*/
$max_value_length = apply_filters( 'ep_facet_meta_value_max_strlen', 100, $meta_key );

$meta_values = array_map(
function ( $value ) use ( $max_value_length ) {
return substr( $value, 0, $max_value_length );
},
$meta_values
);
set_transient( self::TRANSIENT_PREFIX . $meta_key, $meta_values );
}

Expand Down
13 changes: 12 additions & 1 deletion includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1225,10 +1225,11 @@ public function get_distinct_meta_field_keys( $blog_id = null ) {
*
* @since 4.3.0
* @param string $field Field full name. For example: `meta.name.raw`
* @param int $count (Optional) Max number of different distinct values to be returned
* @param int $blog_id (Optional) The blog ID. Sending `null` will use the current blog ID.
* @return array
*/
public function get_all_distinct_values( $field, $blog_id = null ) {
public function get_all_distinct_values( $field, $count = 10000, $blog_id = null ) {
$aggregation_name = 'distinct_values';

$es_query = [
Expand All @@ -1237,6 +1238,16 @@ public function get_all_distinct_values( $field, $blog_id = null ) {
'aggs' => [
$aggregation_name => [
'terms' => [
/**
* Filter the max. number of different distinct values to be returned by Elasticsearch.
*
* @since 4.3.0
* @hook ep_{$indexable_slug}_all_distinct_values
* @param {int} $size The number of different values. Default: 10000
* @param {string} $field The meta field
* @return {string} The new number of different values
*/
'size' => apply_filters( 'ep_' . $this->slug . '_all_distinct_values', $count, $field ),
'field' => $field,
],
],
Expand Down

0 comments on commit 2626fb4

Please sign in to comment.