Question

The documentation for more_like_this shows how you can use it to get more of the same type of content that is similar based on a criteria:

class Post < ActiveRecord::Base
  searchable do
    # The :more_like_this option must be set to true
    text :body, :more_like_this => true
  end
end

post = Post.first

results = Sunspot.more_like_this(post) do
  fields :body
  minimum_term_frequency 5
end

I'm wondering if it's possible to return related items that are a different data type. For example, Videos that are related/similar to Articles.

I guess this depends on whether more_like_this is operating along the lines of "more Articles that are similar to this Article based on a set of criteria" or if it's operating along the lines of "more things that are similar to this Article based on a set of criteria"...

My use-case for this would be if I'm displaying an Article, and I want to show related content on the side of the page - things that might be other Articles, Videos in the same category, or Events on related topics, etc.

Was it helpful?

Solution

http://sunspot.github.com/sunspot/docs/Sunspot.html#more_like_this-class_method

+ (Object) more_like_this(object, *types, &block) Initiate a MoreLikeThis search. MoreLikeThis is a special type of search that finds similar documents using fulltext comparison. The fields to be compared are text fields set up with the :more_like_this option set to true. By default, more like this returns objects of the same type as the object used for comparison, but a list of types can optionally be passed to this method to return similar documents of other types. This will only work for types that have common fields.

example:

  post = Post.first
  Sunspot.more_like_this(post, Post, Page) do
    fields :title, :body
    with(:updated_at).greater_than(1.month.ago)
    facet(:category_ids)
  end

see also: http://sunspot.github.com/sunspot/docs/Sunspot/Query/MoreLikeThis.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top