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

Feature/faceting #7

Merged
merged 30 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7a33bfb
add facet types
jakewrfoster Apr 13, 2022
73e6c6f
add field mapping to controller
jakewrfoster Apr 13, 2022
7b1aaa3
map core fields to adapters
jakewrfoster Apr 13, 2022
6b82b23
add DSL
jakewrfoster Apr 13, 2022
633ef4a
add facet parsing, set/get for config, getter for facet data
jakewrfoster Apr 25, 2022
eef0570
add tax aggregations to vip adapter
jakewrfoster Apr 25, 2022
4d5b869
add tag and category aggs to vip adapter, refactor map_* funcs to ada…
jakewrfoster Apr 25, 2022
90dd6c6
add helpers to get facet data and set up facet configs
jakewrfoster Apr 25, 2022
f554db7
add Facet class
jakewrfoster Apr 25, 2022
757937f
phpcs edits
jakewrfoster Apr 25, 2022
e6be068
add generic adapter allow empty search faceting functionality
jakewrfoster Apr 26, 2022
bde4b23
add facet config to controller
jakewrfoster Apr 26, 2022
dd4c75f
move facet dsl to VIP Adapter class, add func to allow empty search f…
jakewrfoster Apr 26, 2022
1ee22ab
add tag and category faceting config setters
jakewrfoster Apr 26, 2022
eed15a1
add args to post tag and category agg functions
jakewrfoster Apr 26, 2022
7a11fd8
add name setter to facet class
jakewrfoster Apr 26, 2022
51bf61a
inject facet name from config into facet class constructor
jakewrfoster Apr 26, 2022
c3c68f7
please phpcs gods
jakewrfoster Apr 26, 2022
c6a7aed
add TODO to README
jakewrfoster Apr 26, 2022
b4ecc47
add TODO to date DSL
jakewrfoster Apr 26, 2022
c34c6ec
add handling for post_tag item query vars, add TODOs
jakewrfoster Apr 26, 2022
e722737
add enable_post_date_aggregation to controller
jakewrfoster Apr 26, 2022
3a9db46
add facet config to tFacet objects
jakewrfoster Apr 26, 2022
2250ce3
add calendar interval to post_date Facet Type class
jakewrfoster Apr 26, 2022
7abadb9
add Post_Date aggs to qp query request args
jakewrfoster Apr 26, 2022
6d974c8
add post_date aggs to adapter
jakewrfoster Apr 26, 2022
c27cdc1
Merge pull request #9 from alleyinteractive/feature/post-date-facets
jakewrfoster Apr 26, 2022
399a9db
update @see tags
jakewrfoster Apr 26, 2022
f006b5e
add phpcs fixes
jakewrfoster Apr 26, 2022
210e417
Merge branch 'main' into feature/faceting
kevinfodness Apr 28, 2022
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
Prev Previous commit
Next Next commit
phpcs edits
  • Loading branch information
jakewrfoster committed Apr 25, 2022
commit 757937f6ff8d913ae437f477c5ebf25918763619
19 changes: 10 additions & 9 deletions lib/adapters/class-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ abstract class Adapter {
*
* TODO Add param DocBloc with full set of possible array keys.
*
* @param array $facets_config
* @param array $facets_config Array configuration for facets.
* @return void
*/
public function set_facets_config( array $facets_config ) {
Expand All @@ -138,6 +138,7 @@ public function set_facets_config( array $facets_config ) {

/**
* Get the configured facets.
*
* @return array
*/
public function get_facet_config(): array {
Expand Down Expand Up @@ -196,11 +197,11 @@ public function get_facet_data( array $options = [] ): array {

$options = wp_parse_args(
$options,
array(
[
'exclude_current' => true,
'join_existing_terms' => true,
'join_terms_logic' => [],
)
]
);

$facet_data = [];
Expand All @@ -211,7 +212,7 @@ public function get_facet_data( array $options = [] ): array {
continue;
}

$facet_data[ $label ] = $this->facets[ $label ];
$facet_data[ $label ] = $this->facets[ $label ];
$facet_data[ $label ]->items = [];

/*
Expand Down Expand Up @@ -240,7 +241,7 @@ public function get_facet_data( array $options = [] ): array {
}

foreach ( $items as $item ) {
$datum = apply_filters( 'es_extensions_facet_datum', false, $item, $this->facets );
$datum = apply_filters( 'elasticsearch_extensions_facet_datum', false, $item, $this->facets );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need docblocks for any custom action hooks or filters.

if ( false === $datum ) {
$query_vars = [];
$selected = false;
Expand Down Expand Up @@ -353,14 +354,14 @@ public function get_facet_data( array $options = [] ): array {
}
}

return apply_filters( 'es_extensions_facet_data', $facet_data );
return apply_filters( 'elasticsearch_extensions_facet_data', $facet_data );
}

/**
* Get Facet by field
*
* @param string $field Facet field. See get_facet_data for acceptable values.
* @param string $value
* @param string $value Value corresponding to the field.
* @return Facet|null
*/
public function get_facet_data_by( string $field = '', string $value = '' ) {
Expand Down Expand Up @@ -516,7 +517,7 @@ public function map_meta_field( string $meta_key, string $type = '' ): string {
* @param string $field Field to map.
* @return string The mapped field.
*/
public function map_tax_field( string $taxonomy, string $field ): string {
public function map_tax_field( string $taxonomy, string $field ): string {
if ( 'post_tag' === $taxonomy ) {
$field = str_replace( 'term_', 'tag_', $field );
} elseif ( 'category' === $taxonomy ) {
Expand All @@ -529,7 +530,7 @@ public function map_tax_field( string $taxonomy, string $field ): string {
* Pull the facets out of the ES response.
*/
public function parse_facets() {
$this->facets = apply_filters( 'es_extensions_parse_facets', [] );
$this->facets = apply_filters( 'elasticsearch_extensions_parse_facets', [] );
if ( empty( $this->facets ) ) {
if ( ! empty( $this->results['aggregations'] ) ) {
foreach ( $this->results['aggregations'] as $label => $buckets ) {
Expand Down
36 changes: 16 additions & 20 deletions lib/adapters/class-vip-enterprise-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public function filter_ep_query_request_args( $request_args, $path, $index, $typ
// Add our aggregations.
if ( $this->get_aggregate_post_types() ) {
$post_type_facet = new Post_Type();
$dsl['aggs'] = array_merge( $dsl['aggs'], $post_type_facet->request() );
$dsl['aggs'] = array_merge( $dsl['aggs'], $post_type_facet->request() );
}

if ( $this->get_aggregate_categories() ) {
$category_facet = new Category();
$dsl['aggs'] = array_merge( $dsl['aggs'], $category_facet->request() );
$dsl['aggs'] = array_merge( $dsl['aggs'], $category_facet->request() );
}

if ( $this->get_aggregate_tags() ) {
$tag_facet = new Tag();
$tag_facet = new Tag();
$dsl['aggs'] = array_merge( $dsl['aggs'], $tag_facet->request() );
}

Expand All @@ -56,7 +56,7 @@ public function filter_ep_query_request_args( $request_args, $path, $index, $typ
foreach ( $agg_taxonomies as $agg_taxonomy ) {
$dsl['aggs'][ "taxonomy_{$agg_taxonomy}" ] = [
'terms' => [
'size' => 1000,
'size' => 1000,
'field' => "terms.{$agg_taxonomy}.slug",
],
];
Expand Down Expand Up @@ -103,7 +103,7 @@ public function set_field_map(): void {
$this->field_map['post_title.analyzed'] = 'post_title';
$this->field_map['post_type'] = 'post_type.raw';
$this->field_map['post_excerpt'] = 'post_excerpt';
$this->field_map['post_password'] = 'post_password'; // This isn't indexed on VIP;
$this->field_map['post_password'] = 'post_password'; // This isn't indexed on VIP.
$this->field_map['post_name'] = 'post_name.raw';
$this->field_map['post_modified'] = 'post_modified';
$this->field_map['post_modified.year'] = 'modified_date_terms.year';
Expand Down Expand Up @@ -155,15 +155,8 @@ public function setup(): void {
// Filter request args.
add_filter( 'ep_query_request_args', [ $this, 'filter_ep_query_request_args' ], 10, 4 );

// Set aggregations.
add_action( 'ep_valid_response', function ( $response, $query, $query_args ) {
if ( ! empty( $response['aggregations'] ) ) {
$this->set_aggregations( $response['aggregations'] );
}
}, 10, 3 );

// Set Results.
add_action( 'ep_valid_response', [ $this, 'set_results' ], 10, 3 );
// Set Results and aggregations.
add_action( 'ep_valid_response', [ $this, 'set_results' ], 10, 1 );

// Parse face data.
add_action( 'ep_valid_response', [ $this, 'parse_facets' ], 11, 0 );
Expand All @@ -172,14 +165,17 @@ public function setup(): void {
/**
* Set results from last query.
*
* @param $response
* @param $query
* @param $query_args
* @param array $response Response from ES.
* @return void
*/
public function set_results( $response, $query, $query_args ) {
// TODO ensure this is a search? Does EP restrict this to queries with post data?
if ( apply_filters( 'es_extensions_should_set_results', true ) ) {
public function set_results( $response ) {
// Set aggregations if applicable.
if ( ! empty( $response['aggregations'] ) ) {
$this->set_aggregations( $response['aggregations'] );
}

// TODO ensure this is a search and this isn't too broad.
if ( apply_filters( 'elasticsearch_extensions_should_set_results', true ) ) {
$this->results = $response;
}
}
Expand Down
11 changes: 8 additions & 3 deletions lib/class-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,20 @@ public function get_facet_data(): array {
/**
* Get facet data by from adapter.
*
* @param string $name Name of the facet.
* @return Facet|null
*/
public function get_facet_data_by_name( $name = '' ) {
public function get_facet_data_by_name( string $name = '' ) {
return $this->adapter->get_facet_data_by( 'name', $name );
}

/**
* Get facet data by from adapter.
*
* @param string $query_var Query variable sting.
* @return Facet|null
*/
public function get_facet_data_by_query_var( $query_var = '' ) {
public function get_facet_data_by_query_var( string $query_var = '' ) {
return $this->adapter->get_facet_data_by( 'query_var', $query_var );
}

Expand Down Expand Up @@ -144,7 +146,7 @@ public function map_meta_field( string $meta_key, string $type = '' ): string {
* @param string $field Field to map.
* @return string The mapped field.
*/
public function map_tax_field( string $taxonomy, string $field ): string {
public function map_tax_field( string $taxonomy, string $field ): string {
return $this->adapter->map_tax_field( $taxonomy, $field );
}

Expand All @@ -153,6 +155,9 @@ public function map_tax_field( string $taxonomy, string $field ): string {
* Necessary to set up faceting.
*
* TODO Build out the array options in the DocBloc.
*
* @param array $facets_config Config array.
* @return void
*/
public function set_facets_config( $facets_config ) {
$this->adapter->set_facets_config( $facets_config );
Expand Down
8 changes: 4 additions & 4 deletions lib/class-dsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DSL {
* @return array DSL fragment.
*/
public static function all_terms( string $taxonomy, string $field, array $values ): array {
$field = self::$controller->map_tax_field( $taxonomy, $field );
$field = self::$controller->map_tax_field( $taxonomy, $field );
$queries = [];
foreach ( $values as $value ) {
$queries[] = [
Expand Down Expand Up @@ -152,7 +152,7 @@ public static function search_query( string $s ): array {
* @var array
*/
$fields = apply_filters(
'es_extensions_searchable_fields',
'elasticsearch_extensions_searchable_fields',
[
self::$controller->map_field( 'post_title.analyzed' ) . '^3',
self::$controller->map_field( 'post_excerpt' ),
Expand All @@ -175,7 +175,7 @@ public static function search_query( string $s ): array {
/**
* Setter for the controller.
*
* @param Controller $controller
* @param Controller $controller ES Controller class.
* @return void
*/
public static function set_es_controller( Controller $controller ) {
Expand All @@ -192,7 +192,7 @@ public static function set_es_controller( Controller $controller ) {
*/
public static function terms( $field, $values, $args = [] ) {
$field = self::$controller->map_field( $field );
$type = is_array( $values ) ? 'terms' : 'term';
$type = is_array( $values ) ? 'terms' : 'term';

return [
$type => array_merge(
Expand Down
16 changes: 8 additions & 8 deletions lib/class-facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public function __construct( string $label, array $buckets ) {
*/
protected function parse_type() {
if ( 'taxonomy_' === substr( $this->label, 0, 9 ) ) {
$this->type = 'taxonomy';
$this->subtype = $this->query_var = substr( $this->label, 9 );
$this->type = 'taxonomy';
$this->subtype = $this->query_var = substr( $this->label, 9 ); // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
} else {
$this->type = $this->query_var = $this->label;
$this->type = $this->query_var = $this->label; // phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.Found
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public function title(): string {
* @param string $type Facet type.
* @param string $subtype Facet Subtype.
*/
$this->title = apply_filters( 'es_extensions_facet_title', '', $this->label, $this->type, $this->subtype );
$this->title = apply_filters( 'elasticsearch_extensions_facet_title', '', $this->label, $this->type, $this->subtype );
if ( null === $this->title ) {
switch ( $this->type ) {
case 'taxonomy':
Expand All @@ -140,15 +140,15 @@ public function title(): string {
break;

case 'post_type':
$this->title = __( 'Content Type', 'es-extensions' );
$this->title = __( 'Content Type', 'elasticsearch-extensions' );
break;

case 'post_date':
$this->title = __( 'Date', 'es-extensions' );
$this->title = __( 'Date', 'elasticsearch-extensions' );
break;

case 'post_author':
$this->title = __( 'Author', 'es-extensions' );
$this->title = __( 'Author', 'elasticsearch-extensions' );
break;

default:
Expand Down Expand Up @@ -176,7 +176,7 @@ public function get_label_for_bucket( array $bucket ): string {
* @param string $type Facet type.
* @param string $subtype Facet Subtype.
*/
$label = apply_filters( 'es_extensions_facet_bucket_label', null, $bucket, $this->label, $this->type, $this->subtype );
$label = apply_filters( 'elasticsearch_extensions_facet_bucket_label', null, $bucket, $this->label, $this->type, $this->subtype );
if ( null !== $label ) {
return $label;
}
Expand Down
11 changes: 7 additions & 4 deletions lib/facets/class-post-date.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ public function filter( array $values ): array {
foreach ( $values as $date ) {
$gte = date( 'Y-m-d H:i:s', $date ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$lt = date( 'Y-m-d H:i:s', strtotime( date( 'Y-m-d', $date ) . ' + 1 month' ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$should[] = DSL::range( 'post_date', [
'gte' => $gte,
'lt' => $lt,
] );
$should[] = DSL::range(
'post_date',
[
'gte' => $gte,
'lt' => $lt,
]
);
}

return [ 'bool' => [ 'should' => $should ] ];
Expand Down