2012-12-27 197 views
2

The documentation for more_like_this演示了如何使用它来获得更多的同类型的内容基于一个条件类似的:是否可以在不同的数据类型中使用more_like_this?

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 

我不知道是否有可能返回相关的项目是不同的数据类型。例如,与Articles相关/相似的Videos

我想这取决于是否more_like_this沿着“更Articles类似于此Article基于一套标准”或的流水线作业,如果它沿与此类似Article的“更多的东西线运行根据一组标准“...

我的用例是这样的,如果我显示的是Article,我想在页面上显示相关内容 - 可能是其他的ArticlesVideos同一类别,或Events等相关主题等。

回答

4

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

+(对象)more_like_this(对象,*类型,&块)启动一个搜索MoreLikeThis。 MoreLikeThis是一种特殊类型的搜索, 使用全文比较找到类似文档。与 比较的字段是text字段,:more_like_this选项 设置为true。默认情况下,更像这样返回与用于比较的对象类型相同的对象,但可以选择将类型列表 传递给此方法,以返回其他类型的类似文档。 其他类型。这只适用于具有共同字段的类型。

例如:

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 

还看到:http://sunspot.github.com/sunspot/docs/Sunspot/Query/MoreLikeThis.html

+1

谢谢!不幸的是,我遇到了[这个bug](https://github.com/sunspot/sunspot/issues/148),它禁止通过'more_like_this'传递多个对象 - 所以,在你的例子中'页面'...或'Sunspot.more_like_this(post,Post,Page,Video,Event)'作为另一个例子失败,出现'undefined method'more_like_this_fields''错误。 –

相关问题