fix count elements on filters

This commit is contained in:
vnmedeiros 2019-01-16 16:45:12 -02:00
parent 3d9b8a985b
commit 4c4c89194e
1 changed files with 51 additions and 81 deletions

View File

@ -12,6 +12,7 @@ namespace Tainacan;
class Elastic_Press { class Elastic_Press {
public $last_aggregations; public $last_aggregations;
public $facets;
private static $instance = null; private static $instance = null;
public static function get_instance() { public static function get_instance() {
@ -37,6 +38,10 @@ class Elastic_Press {
$this->last_aggregations = $aggregations; $this->last_aggregations = $aggregations;
}, 10, 3); }, 10, 3);
add_action('ep_add_query_log', function($query) {
//error_log($query["args"]["body"]);
});
} }
function filter_args($args, $type) { function filter_args($args, $type) {
@ -95,11 +100,7 @@ class Elastic_Press {
} }
if ( is_array($args['post_type']) ) { if ( is_array($args['post_type']) ) {
$args['aggs'] = array ( $this->facets = [];
'use-filter' => true,
'name' => 'aggs',
'aggs' => array( )
);
foreach ( $args['post_type'] as $cpt ) { foreach ( $args['post_type'] as $cpt ) {
$col = $Tainacan_Collections->fetch_by_db_identifier($cpt); $col = $Tainacan_Collections->fetch_by_db_identifier($cpt);
$_filters = $Tainacan_Filters->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT'); $_filters = $Tainacan_Filters->fetch_by_collection($col, ['posts_per_page' => -1], 'OBJECT');
@ -108,89 +109,58 @@ class Elastic_Press {
$metadatum_options = $filter->get_metadatum()->get_metadata_type_options(); $metadatum_options = $filter->get_metadatum()->get_metadata_type_options();
$taxonomy_id = $metadatum_options['taxonomy_id']; $taxonomy_id = $metadatum_options['taxonomy_id'];
$taxonomy_slug = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id); $taxonomy_slug = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id);
$args['aggs']['aggs'][$taxonomy_slug] = array( $id = $taxonomy_slug;
"terms" => array( $key = "terms.$taxonomy_slug.term_id";
"size" => 10000, $field = "terms.$taxonomy_slug.slug";
"field" => "terms.$taxonomy_slug.slug")
);
} else { } else {
$metadatum_id = $filter->get_metadatum()->get_ID(); $metadatum_id = $filter->get_metadatum()->get_ID();
$args['aggs']['aggs'][$metadatum_id] = array( $id = $metadatum_id;
"terms" => array( $key = "meta.$metadatum_id.raw";
"size" => 10000, $field = "meta.$metadatum_id.raw";
"field" => "meta.$metadatum_id.raw")
);
} }
$this->facets[$id] = ["key" => $key, "field" => $field];
} }
} }
} }
add_filter('ep_formatted_args', array($this, "add_aggs"));
return $args; return $args;
// $metadatum_options = $metadatum->get_metadata_type_options();
// $taxonomy_id = $metadatum_options['taxonomy_id'];
// $taxonomy_slug = Repositories\Taxonomies::get_instance()->get_db_identifier_by_id($taxonomy_id);
//'Tainacan\Metadata_Types\Compound'
//'Tainacan\Metadata_Types\Taxonomy'
//error_log(\json_encode($filters));
//error_log(\json_encode($args['aggs']));
// $args['aggs'] = array (
// 'name' => 'facets',
// 'use-filter' => true,
// 'aggs' => array(
// "tnc_tax_5" => array(
// "terms" => array(
// "size" => 10000,
// "field" => "terms.tnc_tax_5.slug"
// )
// ),
// "teste_meta" => array(
// "terms" => array (
// "size" => 10000,
// "field" => "meta.11.raw"
// )
// )
// )
// );
// {"name":"terms",
// "use-filter":true,
// "aggs": {
// "category": {
// "terms": {
// "size":10000,
// "field":"terms.category.slug"
// }
// },
// "post_tag":{
// "terms":{
// "size":10000,
// "field":"terms.post_tag.slug"
// }
// },
// "post_format":{
// "terms":{
// "size":10000,
// "field":"terms.post_format.slug"
// }
// },
// "tnc_tax_5":{
// "terms":{
// "size":10000,
// "field":"terms.tnc_tax_5.slug"
// }
// }
// }
// }
} }
public function add_aggs($formatted_args) {
$default_filters = $formatted_args['post_filter'];
$aggs = [];
foreach($this->facets as $id => $filter) {
$custom_filter = $default_filters;
$temp = [];
foreach ($custom_filter['bool']['must'] as $item) {
if ( isset($item['bool'])) {
foreach ($item['bool']["must"] as $item_filter) {
if ( !isset( $item_filter["terms"][$filter['key']] ) ) {
$temp[] = $item;
}
}
} elseif ( isset($item['term'])) {
$temp[] = $item;
}
}
$custom_filter['bool']['must'] = $temp;
$aggs[$filter['key']] = [
"filter" => $custom_filter,
"aggs" => array(
$id => array(
"terms"=>array(
"size"=>10000, // size of elements returns
"field"=>$filter['field']
)
)
)
];
}
$formatted_args['aggs'] = $aggs;
return $formatted_args;
}
} // END } // END