在单一品类产品搜索中,如何通过Product Projection Search仅获取Facets结果并屏蔽Results数组?
Absolutely! You can totally pull this off—getting just facet data while skipping the results array to cut down on data transfer. The core trick is telling your search service to return zero results while still calculating and sending back the facet statistics you need. Here are practical implementations based on common search tools:
1. Universal Approach: Set Result Limit to 0
Most search APIs support a parameter like limit, size, or hitsPerPage to control how many results are sent back. Setting this value to 0 will suppress the results array entirely, but the service will still compute and return the facet data you requested.
For example, in a REST API request, your query string would look like this:
?limit=0&facets=variants.attributes.productStyle.en-GB
This will give you a response structure exactly like what you want: an empty results array paired with your desired facet data.
2. Elasticsearch Specific Implementation
If you're using Elasticsearch, use the size: 0 parameter in your query DSL, along with the aggs (aggregations, which map to facets) section:
{ "size": 0, "aggs": { "productStyleFacet": { "terms": { "field": "variants.attributes.productStyle.en-GB" } } } }
Here, size: 0 stops any search hits (your results array) from being returned, while the aggs block calculates the facet values you need. The response will focus solely on the aggregation results.
3. Algolia Specific Implementation
For Algolia, set hitsPerPage: 0 in your search configuration and specify the facets you want:
index.search('', { hitsPerPage: 0, facets: ['variants.attributes.productStyle.en-GB'] });
This call will return an empty hits array (matching your results) and populate the facets field with the requested statistical data.
Bonus Tip
This method doesn't just reduce data transfer—it also lightens the load on your search service, since it doesn't have to process or serialize the matching product results. If your search tool uses slightly different parameter names (like offset paired with limit), just make sure the result count parameter is set to 0, and you'll get the desired outcome.
内容的提问来源于stack exchange,提问作者Sreekanth P M

