I've tried your solution and throws me the same error than without the :group_function :
Post.search params[:q], :group_by => :related_topic_id, :group_function => :attr => NoMethodError: undefined method `bytesize' for :related_topic_id:Symbol I think that somehow the :group_by parameter expects something different than the symbol... If I put the field 'topic_id' as string it runs without crashing but nothing is returned... I'm really confused about the diference between each :group_* parameter and what they expect as a value. Have you get any results doing the grouping like you said? Can you post your define_index? Thank you. On Feb 19, 2:37 pm, Josh <[email protected]> wrote: > Xavi, > > I'm doing something very similar, and it took me some trial and error > to figure it out as well. I think you need the group_function > parameter, but it needs to describe the type of grouping you want, not > the field. Try this: > > @posts = Post.search params[:q], :group_by > => :related_topic_id, :group_function => :attr > > Hopefully that will give you what you're looking for. If so, you'll > have to decide which match you want in the case where there are > multiple posts with the same topic ID. For that you'll need a > combination of the :order and :group_clause params. I found it a > little confusing - it seems that :order will control which of your > grouped items is used, and :group_clause becomes the overall sort for > all your results. > > Good luck, > > - Josh > > On Feb 19, 4:37 am, Xavi Arnaus <[email protected]> wrote: > > > Hi all > > > I'm trying to implement a searching with grouped results as follows: > > > It is a one-to-many relation between Topics and Posts, where each > > Topic has the title and many Posts, which each one has its own text. > > > I'm trying to make the search by Posts so in the results page I can > > point to the Topic but placing the navigator to the Post where the > > match appears. Everything works fine until here: > > > in the Post model: > > > belongs_to :topic > > define_index do > > # fields > > indexes topic.title, :as => :related_topic_title > > indexes text > > indexes created_at, :sorteable => true > > > # attributes > > has created_at > > has topic(:id), :as => :related_topic_id # field is topic_id > > in Posts > > end > > > in the controller, within the search action: > > > @posts = Post.search params[:q] > > > Ok, now the problem: > > If in a Topic there's more than one Post where there is a match, I > > recieve (obviously) all the posts. But as I want to show the results > > by Topic, I need to group all posts by Topic. I tried a lot of > > combinations: > > > @posts = Post.search params[:q], :group_by => :related_topic_id > > [ERROR: undefined method 'bytesize' for symbol] > > @posts = Post.search params[:q], :group_funcion => :related_topic_id > > [ERROR: can't convert nil to Integer] > > @posts = Post.search params[:q], :group_by => 'topic_id' [FAILS: no > > matches!!] > > @posts = Post.search params[:q], :group_funcion > > => :related_topic_id, :group_clause => "created_at DESC" [FAILS: no > > grouping is done] > > > So, as documentation is a little poor in grouping, can anyone help me > > on this? How can I group all Posts in a Topic? Ideally, pointing to > > the first Post of that matches in a Topic is enough... > > > Keep in mind I'm a newbe on Sphinx > > Thank you for your time. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en -~----------~----~----~----~------~----~------~--~---
