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

How to get aggregates #3822

Closed
1 task done
gosixl opened this issue Jan 29, 2024 · 5 comments · Fixed by #3847
Closed
1 task done

How to get aggregates #3822

gosixl opened this issue Jan 29, 2024 · 5 comments · Fixed by #3847
Assignees
Labels
Milestone

Comments

@gosixl
Copy link

gosixl commented Jan 29, 2024

Describe your question

Hello.. getting total aggregations is really undocumented and challenging to find for a new user of this plugin.

I just want to work out, for a product query, I'd like the total counts for the terms for the taxonomies on my woocommerce.

I've tried this code cobbled from googling around.

add_action( 'ep_retrieve_aggregations', function ( array $aggregations ) {
    $agg_response = isset( $aggregations['category_stats']['buckets'] ) ? $aggregations['category_stats']['buckets'] : [];
    print_r($agg_response); // 1
}, 10, 1 );

$query = new WP_Query( [
    's'              => "Retro",
    'post_status'    => 'publish',
    'post_type'      => 'product',
    'ep_integrate'   => true,
    'posts_per_page' => 10,
    'aggs'           => [
       'name' => 'product_cat',
       'aggs' => [
          'terms' => [
             'field' => 'terms.product_cat.slug',
          ],
  
       ],
    ],
    'orderby'        => '_score',
]);

print_r($query); // 2
print_r($GLOBALS["ep_facet_aggs"]);  //3 

Outputs for 1 and 3 are empty arrays. (For 3: PHP Warning: Undefined global variable $ep_facet_aggs)
Output for 2 below (kept to 1 post out for brevity):

{
    "query": {
        "s": "Retro",
        "post_status": "publish",
        "post_type": "product",
        "ep_integrate": true,
        "posts_per_page": 10,
        "aggs": {
            "name": "product_cat",
            "aggs": {
                "terms": {
                    "field": "terms.product_cat.slug"
                }
            }
        },
        "orderby": "_score",
        "suppress_filters": false
    },
    "query_vars": {
        "s": "Retro",
        "post_status": "publish",
        "post_type": "product",
        "ep_integrate": true,
        "posts_per_page": 10,
        "aggs": {
            "name": "product_cat",
            "aggs": {
                "terms": {
                    "field": "terms.product_cat.slug"
                }
            }
        },
        "orderby": "_score",
        "error": "",
        "m": "",
        "p": 0,
        "post_parent": "",
        "subpost": "",
        "subpost_id": "",
        "attachment": "",
        "attachment_id": 0,
        "name": "",
        "pagename": "",
        "page_id": 0,
        "second": "",
        "minute": "",
        "hour": "",
        "day": 0,
        "monthnum": 0,
        "year": 0,
        "w": 0,
        "category_name": "",
        "tag": "",
        "cat": "",
        "tag_id": "",
        "author": "",
        "author_name": "",
        "feed": "",
        "tb": "",
        "paged": 0,
        "meta_key": "",
        "meta_value": "",
        "preview": "",
        "sentence": "",
        "title": "",
        "fields": "",
        "menu_order": "",
        "embed": "",
        "category__in": [],
        "category__not_in": [],
        "category__and": [],
        "post__in": [],
        "post__not_in": [],
        "post_name__in": [],
        "tag__in": [],
        "tag__not_in": [],
        "tag__and": [],
        "tag_slug__in": [],
        "tag_slug__and": [],
        "post_parent__in": [],
        "post_parent__not_in": [],
        "author__in": [],
        "author__not_in": [],
        "search_columns": [],
        "cache_results": false,
        "suppress_filters": false,
        "tax_query": [],
        "ignore_sticky_posts": false,
        "update_post_term_cache": true,
        "update_menu_item_cache": false,
        "lazy_load_term_meta": true,
        "update_post_meta_cache": true,
        "nopaging": false,
        "comments_per_page": "20",
        "no_found_rows": false,
        "search_terms_count": 1,
        "search_terms": [
            "Retro"
        ],
        "search_orderby_title": [
            "wp_posts.post_title LIKE '{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}Retro{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}'"
        ],
        "order": "DESC",
        "spro_possibly_remove_group_by": true,
        "fast-results-key": "fast_results:28f05c461f8d18dfdb988d31dffca47e"
    },
    "tax_query": {
        "queries": [],
        "relation": "AND",
        "queried_terms": [],
        "primary_table": "wp_posts",
        "primary_id_column": "ID"
    },
    "meta_query": {
        "queries": [],
        "relation": null,
        "meta_table": null,
        "meta_id_column": null,
        "primary_table": null,
        "primary_id_column": null
    },
    "date_query": false,
    "request": "\n\t\t\tSELECT SQL_CALC_FOUND_ROWS  wp_posts.*\n\t\t\tFROM wp_posts \n\t\t\tWHERE 1=1  AND (((wp_posts.post_title LIKE '{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}Retro{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}') OR (wp_posts.post_excerpt LIKE '{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}Retro{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}') OR (wp_posts.post_content LIKE '{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}Retro{3cf2a341a5992c9f6200f3846681061357f34b2eebac2ea635d21a9851abad3a}')))  AND (wp_posts.post_password = '')  AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 'publish'))\n\t\t\t\n\t\t\tORDER BY wp_posts.post_date DESC\n\t\t\tLIMIT 0, 10\n\t\t",
    "posts": [
        {
            "ID": 2674855,
            "post_author": "",
            "post_date": "2024-01-27 08:19:10",
            "post_date_gmt": "2024-01-27 08:19:10",
            "post_content": "Puma Lab II Retro. Limited Edition Retro Sneakers.",
            "post_title": "RARE - Puma Lab II 2 FB Lifestyle Retro Shoe Suede Mens Sz 10.5 Trainers",
            "post_excerpt": "",
            "post_status": "publish",
            "comment_status": "open",
            "ping_status": "closed",
            "post_password": "",
            "post_name": "rare-puma-lab-ii-2-fb-lifestyle-retro-shoe-suede-mens-sz-10-5",
            "to_ping": "",
            "pinged": "",
            "post_modified": "2024-01-27 08:19:10",
            "post_modified_gmt": "2024-01-27 08:19:10",
            "post_content_filtered": "",
            "post_parent": 0,
            "guid": "",
            "menu_order": 0,
            "post_type": "product",
            "post_mime_type": "",
            "comment_count": 0,
            "filter": "raw",
            "site_id": 1,
            "permalink": "https:\/\/fluf.io\/catalogue\/rare-puma-lab-ii-2-fb-lifestyle-retro-shoe-suede-mens-sz-10-5\/",
            "terms": {
                "product_cat": [
                    {
                        "term_id": 191,
                        "slug": "trainers",
                        "name": "Trainers",
                        "parent": 178,
                        "term_taxonomy_id": 191,
                        "term_order": 0,
                        "facet": "{\"term_id\":191,\"slug\":\"trainers\",\"name\":\"Trainers\",\"parent\":178,\"term_taxonomy_id\":191,\"term_order\":0}"
                    },
                    {
                        "term_id": 178,
                        "slug": "shoes-socks",
                        "name": "Shoes & Socks",
                        "parent": 0,
                        "term_taxonomy_id": 178,
                        "term_order": 0,
                        "facet": "{\"term_id\":178,\"slug\":\"shoes-socks\",\"name\":\"Shoes & Socks\",\"parent\":0,\"term_taxonomy_id\":178,\"term_order\":0}"
                    }
                ],
                "pa_brand": [
                    {
                        "term_id": 9761,
                        "slug": "rare",
                        "name": "Rare",
                        "parent": 0,
                        "term_taxonomy_id": 9764,
                        "term_order": 0,
                        "facet": "{\"term_id\":9761,\"slug\":\"rare\",\"name\":\"Rare\",\"parent\":0,\"term_taxonomy_id\":9764,\"term_order\":0}"
                    }
                ],
                "pa_colours": [
                    {
                        "term_id": 374,
                        "slug": "multi",
                        "name": "Multi",
                        "parent": 0,
                        "term_taxonomy_id": 374,
                        "term_order": 0,
                        "facet": "{\"term_id\":374,\"slug\":\"multi\",\"name\":\"Multi\",\"parent\":0,\"term_taxonomy_id\":374,\"term_order\":0}"
                    }
                ],
                "pa_condition": [
                    {
                        "term_id": 309,
                        "slug": "used",
                        "name": "Used",
                        "parent": 0,
                        "term_taxonomy_id": 309,
                        "term_order": 0,
                        "facet": "{\"term_id\":309,\"slug\":\"used\",\"name\":\"Used\",\"parent\":0,\"term_taxonomy_id\":309,\"term_order\":0}"
                    }
                ],
                "pa_department": [
                    {
                        "term_id": 271,
                        "slug": "men",
                        "name": "Men",
                        "parent": 0,
                        "term_taxonomy_id": 271,
                        "term_order": 0,
                        "facet": "{\"term_id\":271,\"slug\":\"men\",\"name\":\"Men\",\"parent\":0,\"term_taxonomy_id\":271,\"term_order\":0}"
                    }
                ],
                "product_type": [
                    {
                        "term_id": 101,
                        "slug": "simple",
                        "name": "simple",
                        "parent": 0,
                        "term_taxonomy_id": 101,
                        "term_order": 0,
                        "facet": "{\"term_id\":101,\"slug\":\"simple\",\"name\":\"simple\",\"parent\":0,\"term_taxonomy_id\":101,\"term_order\":0}"
                    }
                ]
            },
            "meta": {
                "_manage_stock": [
                    {
                        "value": "yes",
                        "raw": "yes",
                        "boolean": true,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_stock_status": [
                    {
                        "value": "instock",
                        "raw": "instock",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_stock": [
                    {
                        "value": "1",
                        "raw": "1",
                        "long": 1,
                        "double": 1,
                        "boolean": true,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_regular_price": [
                    {
                        "value": "62.82",
                        "raw": "62.82",
                        "long": 62,
                        "double": 62.82,
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "total_sales": [
                    {
                        "value": "0",
                        "raw": "0",
                        "long": 0,
                        "double": 0,
                        "boolean": false
                    }
                ],
                "_tax_status": [
                    {
                        "value": "taxable",
                        "raw": "taxable",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_tax_class": [
                    {
                        "value": "",
                        "raw": "",
                        "boolean": false
                    }
                ],
                "_backorders": [
                    {
                        "value": "no",
                        "raw": "no",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_sold_individually": [
                    {
                        "value": "no",
                        "raw": "no",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_virtual": [
                    {
                        "value": "no",
                        "raw": "no",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_downloadable": [
                    {
                        "value": "no",
                        "raw": "no",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_wc_average_rating": [
                    {
                        "value": "0",
                        "raw": "0",
                        "long": 0,
                        "double": 0,
                        "boolean": false
                    }
                ],
                "_product_attributes": [
                    {
                        "value": "a:5:{s:13:\"pa_department\";a:6:{s:4:\"name\";s:13:\"pa_department\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:0;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:8:\"pa_brand\";a:6:{s:4:\"name\";s:8:\"pa_brand\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:1;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:7:\"pa_size\";a:6:{s:4:\"name\";s:7:\"pa_size\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:2;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:10:\"pa_colours\";a:6:{s:4:\"name\";s:10:\"pa_colours\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:3;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:12:\"pa_condition\";a:6:{s:4:\"name\";s:12:\"pa_condition\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:4;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}}",
                        "raw": "a:5:{s:13:\"pa_department\";a:6:{s:4:\"name\";s:13:\"pa_department\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:0;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:8:\"pa_brand\";a:6:{s:4:\"name\";s:8:\"pa_brand\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:1;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:7:\"pa_size\";a:6:{s:4:\"name\";s:7:\"pa_size\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:2;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:10:\"pa_colours\";a:6:{s:4:\"name\";s:10:\"pa_colours\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:3;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}s:12:\"pa_condition\";a:6:{s:4:\"name\";s:12:\"pa_condition\";s:5:\"value\";s:0:\"\";s:8:\"position\";i:4;s:10:\"is_visible\";i:1;s:12:\"is_variation\";i:0;s:11:\"is_taxonomy\";i:1;}}",
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_product_version": [
                    {
                        "value": "8.5.2",
                        "raw": "8.5.2",
                        "boolean": false,
                        "date": "2024-01-29",
                        "datetime": "2024-01-29 08:05:02",
                        "time": "08:05:02"
                    }
                ],
                "_price": [
                    {
                        "value": "62.82",
                        "raw": "62.82",
                        "long": 62,
                        "double": 62.82,
                        "boolean": false,
                        "date": "1970-01-01",
                        "datetime": "1970-01-01 00:00:01",
                        "time": "00:00:01"
                    }
                ],
                "_variations_skus": []
            },
            "elasticsearch": true
        },


Code of Conduct

  • I agree to follow this project's Code of Conduct
@gosixl
Copy link
Author

gosixl commented Jan 29, 2024

So what i left out above is that I was running that code from a php file in the terminal (wp-load included!), but it seems i needed to add this to ensure facets would work: add_filter( 'ep_is_facetable', '__return_true' ); If you search for this you will see there's an exclusion against WP CLI for faceting.

Took inspiration from here @ #3352

I still think the aggregations functionality could be better documented!

@felipeelia felipeelia added this to the 5.1.0 milestone Feb 6, 2024
@felipeelia
Copy link
Member

felipeelia commented Feb 23, 2024

Hi @gosixl, you are totally right. The aggregations functionality is something we need to document better. For now, can you please try to debug it using this snippet? Thanks!

add_filter(
	'ep_es_query_results',
	function ( $results, $response, $query, $query_args, $query_object ) {
		print_r( $results );
		print_r( $response );
		return $results;
	},
	10,
	5
);

The $query_object variable is your WP_Query object, so you can create a relationship between them in that function if you need to.

@gosixl
Copy link
Author

gosixl commented Feb 23, 2024

I found $GLOBALS['ep_facet_aggs'] to be an okay solution. Is that a good idea?

@felipeelia
Copy link
Member

That works @gosixl, but only because you have add_filter( 'ep_is_facetable', '__return_true' ); there. If you don't plan to use facets/filters, the ep_es_query_results approach would be preferable.

@gosixl
Copy link
Author

gosixl commented Feb 27, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants