Order by intersection

37 views
Skip to first unread message

Bruno Azisaka Maciel

unread,
May 23, 2012, 11:13:43 AM5/23/12
to Sunspot
Hi,

I have the following scenario:

class Category
  # fields: name
  has_and_belongs_to_many :venues
end

class Venue
  has_and_belongs_to_many :categories

  searchable do
    string :categories_names, multiple: true, stored: true
  end
  
  def categories_names
    categories.map(&:name).flatten
  end
end

I want to order the venues by the intersection of the category names.

Right now I'm doing the following:

class Venue
  def related_venues
    search = Venue.search do
      with(:categories_names).any_of(categories_names)
    end
    
    hits = search.hits.sort_by { |hit| (hit.stored("categories_names") & categories_names).size }
    
    hits.reverse.map(&:result)
  end
end

But I wanted to know if there is a better way to do this intersection. Maybe creating a dynamic field? I really don't know :/

Thank you in advance.
-- 
Bruno Azisaka Maciel

Mauro Asprea

unread,
May 23, 2012, 12:33:02 PM5/23/12
to ruby-s...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Sunspot" group.
To post to this group, send email to ruby-s...@googlegroups.com.
To unsubscribe from this group, send email to ruby-sunspot...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-sunspot?hl=en.

--
Mauro Asprea

E-Mail: mauro...@gmail.com
Mobile: +34 654297582
Skype: mauro.asprea

Bruno Azisaka Maciel

unread,
May 23, 2012, 1:22:26 PM5/23/12
to ruby-s...@googlegroups.com
Ok, but order_by..what? :)

I don't know exactly what I should order by.

-- 
Bruno Azisaka Maciel

postbox-contact.jpg

Mauro Asprea

unread,
May 23, 2012, 1:45:24 PM5/23/12
to ruby-s...@googlegroups.com
Hahah, sorry about that. You want to know how many categories "hits" have and order by that number right?

Why are you doing this with sunspot? I think it would be better to jus get the "count" of the association and order with it...

like venue.categories.count you can even cache that in counter or something like that...

May 23, 2012 7:22 PM
Ok, but order_by..what? :)

I don't know exactly what I should order by.

-- 
Bruno Azisaka Maciel

On Wednesday, May 23, 2012 at 1:33 PM, Mauro Asprea wrote:


--
You received this message because you are subscribed to the Google Groups "Sunspot" group.
To post to this group, send email to ruby-s...@googlegroups.com.
To unsubscribe from this group, send email to ruby-sunspot...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-sunspot?hl=en.
May 23, 2012 6:33 PM

Bruno Azisaka Maciel

unread,
May 23, 2012, 1:49:15 PM5/23/12
to ruby-s...@googlegroups.com
Not really.

Let's say I have 3 venues with the categories:

venue1.categories = ["Restaurant", "Japanese", "Fusion"]
venue2.categories = ["Restaurant", "Italian"]
venue3.categories = ["Restaurant", "Japanese"]

The order by intersection for the venues related to the venue1 is:

venue3 -> venue2

Because venue3 has "Restaurant" and "Japanese" and venue2 has only "Restaurant"

It's not just a count I guess.

-- 
Bruno Azisaka Maciel

postbox-contact.jpg
postbox-contact 2.jpg

Mauro Asprea

unread,
May 23, 2012, 2:14:59 PM5/23/12
to ruby-s...@googlegroups.com
Ohh I see. I can't see a way of doing that directly using solr, but it's just me :S Solr would help you to show facets for a determined filter, but I don't see how it can order by a value that is dependent of a "graph"

I think the way your are doing it looks fine. What issues are you having with it?

Maybe with something like this could be done? http://loose-bits.com/2011/09/20/pivot-facets-solr.html I don't think that's already implemented in sunspot though...
May 23, 2012 7:49 PM
Reply all
Reply to author
Forward
0 new messages