轨道与黑子面和过滤 [英] Rails & Sunspot facets and filtering

查看:83
本文介绍了轨道与黑子面和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有点麻烦,所以我感谢别人能提供的任何帮助.

Pretty much a noobie here, so I appreciate any help someone can give.

我正在尝试通过Sunspot在我的网站上添加搜索内容.瑞安(Ryan)刚刚发布了一个很棒的Railscast,让我开始了: http://railscasts.com/episodes/278-search-with -sunspot .我得到了工作,并能够添加其他方面.我的问题是,构面彼此独立.如果我在3个不同的属性上具有3个方面,则一旦选择了一个方面,当我选择一个方面时,我只想显示同时落入这两个方面的结果.截至目前,它只是从一个方面切换到另一个方面.我觉得这应该没那么难,但是我不知道该怎么做.

I'm trying to add faceting to the search on my site through Sunspot. Ryan just released a great Railscast which got me started: http://railscasts.com/episodes/278-search-with-sunspot. I got that working and was able to add additional facets. My problem is that the facets are independent of each other. If I have 3 facets on 3 different attributes, when I select a facet once I already have on selected, I would like to display only results falling into both of those facests. As of now, it just switches from one facet to the other. I feel like this shouldn't be that difficult, but I can't figure out how to do it.

我确实找到了本教程: http://blog.upubly. com/2011/01/06/using-sunspot-in-your-views/,我认为这是我想要的.我试图使此工作正常进行,但是即使我尝试仅从一个方面进行工作,也没有列出任何结果.只是构面名称,然后别无其他.

I did find this tutorial: http://blog.upubly.com/2011/01/06/using-sunspot-in-your-views/ which I think is doing what I want. I tried to get this working but, even when I attempt to make it work with just one facet I don't any results listed. Just the facet name and then nothing else.

有想法吗?

谢谢!

更新

这是我要执行的操作的代码示例:

Here is the code samples of what I am trying to do:

调整Railscasts代码我得到了:

Adjusting the Railscasts code I got this:

在我的StylesController中:

In my StylesController:

 def index
  @search = Style.search do
  fulltext params[:search]
  facet :departmental, :seasonal, :classifier
  with(:departmental, params[:department]) if params[:department].present?
  with(:classifier, params[:classification]) if params[:classification].present?
  with(:seasonal, params[:season]) if params[:season].present?
end

在样式索引"视图中(我知道我需要对此进行压缩)

In my Style Index view (I know I need to condense this)

  = form_tag styles_path, :method => :get do
    %p
      = text_field_tag :search, params[:search]
      = submit_tag "Search", :name => nil
  #facets
    %h4 Departments
    %ul
      - for row in @search.facet(:departmental).rows
        %li
          - if params[:department].blank?
            = link_to row.value, :department => row.value
            (#{row.count})
          - else
            %strong= row.value
            (#{link_to "remove", :department => nil})
    %h4 Classifications
    %ul
      - for row in @search.facet(:classifier).rows
        %li
          - if params[:classification].blank?
            = link_to row.value, :classification => row.value
            (#{row.count})
          - else
            %strong= row.value
            (#{link_to "remove", :classification => nil})
    %h4 Seasons
    %ul
      - for row in @search.facet(:seasonal).rows
        %li
          - if params[:season].blank?
            = link_to row.value, :season => row.value
            (#{row.count})
          - else
            %strong= row.value
            (#{link_to "remove", :season => nil})

在我的样式模型中:

     searchable do
       text :number, :description, :department, :classification, :season
       string :departmental
       string :classifier
       string :seasonal
     end

     def departmental
       self.department
     end

     def classifier
       self.classification
     end

     def seasonal
       self.season
     end

和我的版本代码配对,试图使季节性"方面起作用:

And my version of the upubly code, paired down to just try to get the "seasonal" facet working:

我保留了与示例相同的Search Partial,Search Model和SearchHelper.我试图弄乱Helper,因为我的Facets将提取文本值,而不仅仅是其他模型的ID,但无济于事.我没有将各种属性设置为单独的模型,因为我认为我不需要该功能,但是我开始考虑另辟.径.

I left the the Search Partial, the Search Model and the SearchHelper the same as in the example. I tried to mess with the Helper as my Facets will be pulling text values, not just IDs of other Models, but to no avail. I don't have my various attributes set up as individual Models as I didn't think I needed that functionality, but I am starting to think otherwise.

StylesController:

StylesController:

  def index
@title = "All styles"
@search = search = Search.new(params[:search]) # need to set local variable to pass into search method
@search.url = styles_path
@search.facets = [:seasonal]

@solr_search = Style.search do
  keywords search.query
  with(:seasonal, true)
  search.facets.each do |item|
    facet(item)
    with(:seasonal, params[:season]) if params[:season].present?
  end
  any_of do
      # filter by facets
      search.facets.each do |item|
        with(item).all_of( params[item].try(:split, "-") ) if params[item].present?
      end
  end  
  paginate(:page => params[:page], :per_page => 10)
end

再次感谢您的帮助.绝对是菜鸟,但真的很喜欢构建此网站的过程. Stackoverflow对我来说已经是一个巨大的帮助,所以我应该感谢在这里发布答案的每个人.

Again, I appreciate the help. Definitely a noob, but really enjoying the process of building this site. Stackoverflow has been a HUGE help for me already, so I owe everybody who posts answers on here a big-time thank you.

推荐答案

我自己需要答案,看到网上似乎没有其他答案,我决定尝试解决这个问题.我自己.

I needed the answer to this myself, and seeing as there seems to be nothing else on the web about it, I decided I'd try to figure it out myself.

首先,我通过逻辑得出结论,即控制器可以处理多个方面,没有理由不能这样做,我记得关于ruby的最好之处是它是最易读的代码,请尝试阅读第一个控制器并且您会看到它起作用了.我通过在url中手动输入查询字符串进行了测试,该查询字符串返回了预期的结果.因此,一旦我弄清了这一点,便知道问题出在我的视线之内(这使我变得脸红,因为现在已经很明显了)

First I came to the conclusion through logic, that the controller can handle multiple facets and there's no reasons it cannot, I remembered that the best part about ruby is that it is the most human readable code, try to read your first controller and you'll see that it makes sense that it works. I tested this by manually entering in a query string in url, which returned expected results. Therefore, once I figured that out, I knew the issue resided in my view (which made me facepalm because it's fairly obvious now)

您的示例比我的示例要复杂得多,我的答案可能不会100%满足所有要求,但我很确定它已经接近.另外,在我看来,您模型中有关部门"等的代码有点多余

Your example is significantly more complex than mine, and my answer might not 100% meet every requirement but I'm pretty sure it's close. Also your code in your model regarding "departmental" etc is a little redundant in my view

def index
  @search = Style.search do
  fulltext params[:search]
  facet :departmental, :seasonal, :classifier
  with(:departmental, params[:department]) if params[:department].present?
  with(:classifier, params[:classification]) if params[:classification].present?
  with(:seasonal, params[:season]) if params[:season].present?
end

查看

%h4 Departments
%ul
  - for row in @search.facet(:departmental).rows
    %li
      - if params[:department].blank?
        = link_to row.value, styles_path(
          :department => row.value,
          :classification => (params[:classification] unless params[:season].blank?),
          :season => (params[:season] unless params[:season].blank?))
        (#{row.count})
      - else
        %strong= row.value
        = link_to "remove", styles_path(
          :department => nil,
          :classification => (params[:classification] unless params[:season].blank?),
          :season => (params[:season] unless params[:season].blank?))

这篇关于轨道与黑子面和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆