At times we may need to group our search results by an attribute. For example, to show monthly statistics about our blog posts we will need to group the posts by publish_date
, or to show count of books by a particular author we will need to group the search results by author.
Sphinx offers a grouping mode which is enabled with SetGroupBy()
API call. All matches are assigned to different groups based on group-by value when grouping is used.
Different functions are available to compute the group-by value:
SPH_GROUPBY_DAY:
Extracts year, month, and day in YYYYMMDD format from thetimestamp
attributeSPH_GROUPBY_WEEK:
Extracts year and first day of the week number inYYYYNNN
format fromtimestamp
SPH_GROUPBY_MONTH:
Extracts year and month inYYYYMM
format fromtimestamp
SPH_GROUPBY_YEAR:
Extracts year inYYYY
format fromtimestamp
The function to be used for grouping is:
SetGroupBy ( $attribute, $func, $groupsort...