正在考虑 Sphinx 分面搜索实施示例?

发布于 2024-10-21 02:31:33 字数 1156 浏览 5 评论 0原文

我正在整理一个存储库类型的 Rails 3 站点。 我已经安装了 Thinking Sphinx 并在我的网站上运行,因为我可以输入像 localhost:3000/articles?search=test&page=2 这样的网址,它将返回预期的结果。

我是 Rails 新手(以及一般的 Web 开发人员);我认为我可以管理模型和控制器方面的问题,但到目前为止的观点让我感到困惑。我有一个侧边栏,我想用作搜索界面。我最接近的是这个(作为侧边栏部分的一部分呈现):

<% form_tag @search, :method => :get do %> 
    <%= text_field_tag :search, params[:search] %> 
    <%= submit_tag "Search"%> 
<% end %>

搜索方法位于我的articles#index控制器中,当我在浏览器指向该页面时测试它(路由为/articles< /code>),它按预期工作,但有这个奇怪的网址:localhost:3000/articles?utf8=✓&search=test&commit=Search。当浏览器第一次指向根路径时,什么也没有发生。

所以,我认为这些是我需要解决的主要问题:

  1. 编辑 - 已解决(见下文)
  2. 我应该将搜索方法移至它们自己的控制器,还是应该将它们作为文章控制器的一部分?目前,Article 将是唯一被索引的模型。
  3. 编辑 - 已解决(见下文)
  4. 有谁有使用 Rails 3 和 Thinking Sphinx 的分面搜索视图的任何好的示例代码吗?就像我说的,我是一个新手,对浏览视图实现的文档感到有点慌张。然而,只要代码相当完整,我就相当擅长阅读和解释代码。

提前致谢!

已解决:

  1. 如何让“搜索”按钮在尝试搜索之前调用索引方法? (我已经通过用 articles_path 替换 @search 解决了这个问题)。

  2. 使用will_paginate解决了,我以前遇到过麻烦,但现在似乎可以工作。

I am putting together a repository-type rails 3 site.
I have Thinking Sphinx installed and working on my site, insomuch as I can enter urls like localhost:3000/articles?search=test&page=2 and it will return the expected results.

I'm new to Rails (and web dev in general); I think I can manage the model and controller aspects of this, but the views so far have me stumped. I have a sidebar I would like to use as the search interface. The closest I have come is this (rendered as part of a sidebar partial):

<% form_tag @search, :method => :get do %> 
    <%= text_field_tag :search, params[:search] %> 
    <%= submit_tag "Search"%> 
<% end %>

The search method is in my articles#index controller, and when I test it when the browser is pointed to that page (routed as /articles), it works as expected, but with this odd url: localhost:3000/articles?utf8=✓&search=test&commit=Search. When the browser is first pointed to the root path, nothing happens.

So, I think these are the main issues I need to address:

  1. EDIT - solved (see below)
  2. Should I move the search methods to their own controller, or should they be part of the articles controller? For now, Article will be the only model indexed.
  3. EDIT - solved (see below)
  4. Does anyone have any good example code of a faceted search view using Rails 3 and Thinking Sphinx? Like I said, I am something of a neophyte and am a little flustered by the documentation that skims by the view implementation. However, I am fairly adept at reading and interpreting code as long as it is reasonably complete.

Thanks in advance!

Solved:

  1. How do I make the 'Search' button call the index method before trying to search? (I have solved this by replacing @search with articles_path).

  2. Solved using will_paginate, which I had trouble with before, but which seems to be working now.

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

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

发布评论

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

评论(1

缺⑴份安定 2024-10-28 02:31:33

嘿,
这是我切换到 solr 之前我的网站如何工作的摘录

产品有很多类别,我们告诉 sphinx 我们希望将它们索引为构面

class Product < ActiveRecord::Base {    
    has_many :categorisations, :dependent => :destroy
    has_many :categories, :through => :categorisations        

 define_index do
    indexes product_name,
    indexes description
    indexes categories(:name), :as => :category,:facet => true      
 end 
}

结果控制器

class ResultsController < ApplicationController
  def index
      @facets = Product.facets params[:qt], :conditions => {:category => params[:category}},:page => params[:page], :per_page => 20
      @products = @facets.for
  end
end

然后在视图中您可以执行类似的操作

<% @facets.each do |facet, facet_options| %>
  <span><%= facet %></span>
  <ul>
  <% facet_options.each do |option, count| %>
    <li><%= link_to "#{option} (#{count})",
      :params => {facet => option, :page => 1} %></li>
  <% end %>
  </ul>
<% end %>

Hey,
This is an extract of how my site worked before i switched to solr

Product has many categories, we tell sphinx that we want to index them as facets

class Product < ActiveRecord::Base {    
    has_many :categorisations, :dependent => :destroy
    has_many :categories, :through => :categorisations        

 define_index do
    indexes product_name,
    indexes description
    indexes categories(:name), :as => :category,:facet => true      
 end 
}

Results Controller

class ResultsController < ApplicationController
  def index
      @facets = Product.facets params[:qt], :conditions => {:category => params[:category}},:page => params[:page], :per_page => 20
      @products = @facets.for
  end
end

And then in the view you can do something like

<% @facets.each do |facet, facet_options| %>
  <span><%= facet %></span>
  <ul>
  <% facet_options.each do |option, count| %>
    <li><%= link_to "#{option} (#{count})",
      :params => {facet => option, :page => 1} %></li>
  <% end %>
  </ul>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文