Method: Sphinx::Client#SetGroupBy

Defined in:
lib/sphinx/sphinx/client.rb

#SetGroupBy(attribute, func, groupsort = '@group desc') ⇒ Object

Set grouping attribute and function.

In grouping mode, all matches are assigned to different groups based on grouping function value.

Each group keeps track of the total match count, and the best match (in this group) according to current sorting function.

The final result set contains one best match per group, with grouping function value and matches count attached.

Groups in result set could be sorted by any sorting clause, including both document attributes and the following special internal Sphinx attributes:

  • @id - match document ID;

  • @weight, @rank, @relevance - match weight;

  • @group - groupby function value;

  • @count - amount of matches in group.

the default mode is to sort by groupby value in descending order, ie. by ‘@group desc’.

‘total_found’ would contain total amount of matching groups over the whole index.

WARNING: grouping is done in fixed memory and thus its results are only approximate; so there might be more groups reported in total_found than actually present. @count might also be underestimated.

For example, if sorting by relevance and grouping by “published” attribute with SPH_GROUPBY_DAY function, then the result set will contain one most relevant match per each day when there were any matches published, with day number and per-day match count attached, and sorted by day number in descending order (ie. recent days first).



443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/sphinx/sphinx/client.rb', line 443

def SetGroupBy(attribute, func, groupsort = '@group desc')
  assert { attribute.instance_of? String }
  assert { groupsort.instance_of? String }
  assert { func == SPH_GROUPBY_DAY \
        || func == SPH_GROUPBY_WEEK \
        || func == SPH_GROUPBY_MONTH \
        || func == SPH_GROUPBY_YEAR \
        || func == SPH_GROUPBY_ATTR \
        || func == SPH_GROUPBY_ATTRPAIR }

  @groupby = attribute
  @groupfunc = func
  @groupsort = groupsort
end