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

Introduce native facet mappings #2071

Merged
merged 9 commits into from
Apr 30, 2021
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
20 changes: 19 additions & 1 deletion includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,29 @@ public function facet_query( $query ) {

$facets = [];

/**
* Retrieve aggregations based on a custom field. This field must exist on the mapping.
* Values available out-of-the-box are:
* - slug (default)
* - term_id
* - name
* - parent
* - term_taxonomy_id
* - term_order
* - facet (retrieves a JSON representation of the term object)
*
* @since 3.6.0
* @hook ep_facet_use_field
* @param {string} $field The term field to use
* @return {string} The chosen term field
*/
$facet_field = apply_filters( 'ep_facet_use_field', 'slug' );

foreach ( $taxonomies as $slug => $taxonomy ) {
$facets[ $slug ] = array(
'terms' => array(
'size' => apply_filters( 'ep_facet_taxonomies_size', 10000, $taxonomy ),
'field' => 'terms.' . $slug . '.slug',
'field' => 'terms.' . $slug . '.' . $facet_field,
),
);
}
Expand Down
17 changes: 12 additions & 5 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ private function prepare_terms( $post ) {
'term_taxonomy_id' => $term->term_taxonomy_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@felipeelia Can you check if there a reason why we only collect the term_taxonomy_id here and not when getting parent terms in line 462 below? I have checked annotations and this difference has been around since tt_ids were introduced in #840

I've now bumped into an issue in hierarchical taxonomies in which the new native facet is stored twice because of this. It's stored once with term_taxonomy_id (created here) and once without it (created in get_parent_terms() below, whenever a child term is stored).

Adding term_taxonomy_id to parent terms fixes the issue, but I wanted to check if there is any reason why it didn't have that in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

I think it was just something we missed until now, @moraleida.

@brandwaffle can we aim to have this merged in 3.6.0?

'term_order' => (int) $this->get_term_order( $term->term_taxonomy_id, $post->ID ),
);

$terms_dic[ $term->term_id ]['facet'] = json_encode( $terms_dic[ $term->term_id ] );

if ( $allow_hierarchy ) {
$terms_dic = $this->get_parent_terms( $terms_dic, $term, $taxonomy->name, $post->ID );
}
Expand Down Expand Up @@ -457,12 +460,16 @@ private function get_parent_terms( $terms, $term, $tax_name, $object_id ) {
}
if ( ! isset( $terms[ $parent_term->term_id ] ) ) {
$terms[ $parent_term->term_id ] = array(
'term_id' => $parent_term->term_id,
'slug' => $parent_term->slug,
'name' => $parent_term->name,
'parent' => $parent_term->parent,
'term_order' => $this->get_term_order( $parent_term->term_taxonomy_id, $object_id ),
'term_id' => $parent_term->term_id,
'slug' => $parent_term->slug,
'name' => $parent_term->name,
'parent' => $parent_term->parent,
'term_taxonomy_id' => $parent_term->term_taxonomy_id,
'term_order' => $this->get_term_order( $parent_term->term_taxonomy_id, $object_id ),
);

$terms[ $parent_term->term_id ]['facet'] = json_encode( $terms[ $parent_term->term_id ] );

}
return $this->get_parent_terms( $terms, $parent_term, $tax_name, $object_id );
}
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/5-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@
'slug' => array(
'type' => 'keyword',
),
'facet' => array(
'type' => 'keyword',
),
'term_order' => array(
'type' => 'long',
),
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/5-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
'slug' => array(
'type' => 'keyword',
),
'facet' => array(
'type' => 'keyword',
),
'term_order' => array(
'type' => 'long',
),
Expand Down
3 changes: 3 additions & 0 deletions includes/mappings/post/7-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
'slug' => array(
'type' => 'keyword',
),
'facet' => array(
'type' => 'keyword',
),
'term_order' => array(
'type' => 'long',
),
Expand Down
4 changes: 4 additions & 0 deletions includes/mappings/post/pre-5-0.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@
'type' => 'string',
'index' => 'not_analyzed',
),
'facet' => array(
'type' => 'string',
'index' => 'not_analyzed',
),
'term_order' => array(
'type' => 'long',
),
Expand Down