铁轨和太阳黑子面和过滤

发布于 2024-11-29 12:58:09 字数 3589 浏览 1 评论 0原文

这里几乎是菜鸟,所以我很感谢有人可以提供的任何帮助。

我正在尝试通过太阳黑子向我的网站上的搜索添加分面。 Ryan 刚刚发布了一个很棒的 Railscast,它让我开始了:http://railscasts.com/episodes/278-search-with -太阳黑子。我成功了并且能够添加额外的方面。我的问题是这些方面是相互独立的。如果我在 3 个不同的属性上有 3 个方面,当我选择一个已经选择的方面时,我想仅显示属于这两个方面的结果。到目前为止,它只是从一个方面切换到另一个方面。我觉得这应该不难,但我不知道该怎么做。

我确实找到了这个教程:http://blog.upubly.com/2011 /01/06/using-sunspot-in-your-views/ 我认为这是在做我想做的事。我试图让它发挥作用,但是,即使当我尝试使其仅在一个方面发挥作用时,我也没有列出任何结果。只是方面名称,然后没有其他内容。

想法?

谢谢你!!

更新

这是我想要做的代码示例:

调整 Railscasts 代码我得到了这个:

在我的 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

在我的样式索引视图中(我知道我需要压缩它)

  = 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

以及我的版本upubly 代码,配对以尝试让“季节性”方面正常工作:

我将搜索部分、搜索模型和 SearchHelper 与示例中的相同。我试图干扰 Helper,因为我的 Facets 将拉取文本值,而不仅仅是其他模型的 ID,但无济于事。我没有将各种属性设置为单独的模型,因为我认为我不需要该功能,但我开始不这么认为。

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 已经给了我很大的帮助,所以我要向所有在这里发布答案的人致以深深的谢意。

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

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.

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.

Thoughts?

Thank you!!

UPDATE

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

Adjusting the Railscasts code I got this:

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})

In my Style Model:

     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:

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:

  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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2024-12-06 12:58:09

我自己需要这个问题的答案,并且鉴于网络上似乎没有其他相关内容,我决定自己尝试找出答案。

首先,我通过逻辑得出结论,控制器可以处理多个方面,并且没有理由不能,我记得 ruby​​ 最好的部分是它是最人类可读的代码,尝试阅读你的第一个控制器,然后你'你会发现它确实有效。我通过在 url 中手动输入查询字符串来对此进行测试,该字符串返回了预期结果。因此,一旦我弄清楚了,我就知道问题出在我的观点中(这让我捂脸,因为现在已经很明显了)

你的例子比我的要复杂得多,我的答案可能不会100%满足所有要求,但我很确定已经很接近了。 您的模型中有关“部门”等的代码有点多余

另外,在我看来

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?))

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.

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)

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

Controller

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

View

%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?))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文