0

I'm trying to do a search with TS in which the results are grouped by an attribute (in the example: category).

A structure like this (doesn't have to be arrays):

[[id: 1, name: this, category: foo] [id: 2, name: this, category: foo]],
[[id:3, name: this, category: bar], [id:4, name: this, category: bar]]

I think I could get the results sorted by the attribute and then go over the results generating the structure I want by the attribute but what I'm trying to see if it's possible is that TS does this for me without having to use rails to go over every result item.

I need this structure to display the result elements categorised by the attribute.

I've tried using GROUP_BY but it returns only 1 element per attribute which makes me think I either don't understand the GROUP_BY or I'm not doing the query right:

Video.search(Search.build_options.merge({:conditions => {:name => "this"}, group_by: :category}))

I'm using the current versions of Sphinx and Thinking Sphinx,

vicocas
  • 249
  • 2
  • 7
  • 1
    one element per group, is kinda of the definition of grouping. You get one per group, along with how many items in that group. Its an aggregation function. It sounds like maybe you not wanting to actully 'group', but maybe just 'order by' so that rows of the came category are adjacent? - if that doesnt explain, maybe can elaborate a bit more on the actual structure you **looking for** – barryhunter Jan 19 '17 at 18:28

1 Answers1

1

As Barry's noted in his comments, Sphinx's approach to grouping is to return one record for each grouped attribute value, rather than a collection of results for that value. Thus, Thinking Sphinx behaves this way as well.

If you want collections of results for each value, you're going to have to construct that yourself - you definitely can't rely on TS to do it for you.

pat
  • 16,116
  • 5
  • 40
  • 46