【问题标题】:Problems Using Thinking Sphinx with Rails 3.2.1在 Rails 3.2.1 中使用 Thinking Sphinx 的问题
【发布时间】:2012-02-29 04:12:48
【问题描述】:

我正在尝试实现 Thinking Sphinx 2.0.10,据我了解,它与 Rails 3 兼容。我对使用 Ruby on Rails 进行编程非常陌生。我浏览了很多关于 StackOverflow 的文章,但找不到解决问题的方法。

我在 Gemfile 中安装了没有 :require 参数的 gem。

gem 'thinking-sphinx', '2.0.10'

我有一个可用的 Rails 应用程序,它显示了一个我想添加搜索的列表。

这是我在模型文件中定义索引的代码。

define_index do
  indexes :email, :sortable => true
  indexes :name, :sortable => true
  indexes microposts.content, :as => :micropost_content
end

这是我的控制器文件中的 Rails 代码。注释掉的行是正在工作的原始代码。我有一个默认的 will_paginate 我认为在应用程序控制器中每页 15 条记录。

def index
  @users = User.search params[:search], :per_page => 15
  # @users = User.paginate(page: params[:page])
end

这是我添加到索引页的搜索框。

<p>
  <%= text_field_tag :search, params[:search] %>
  <%= submit_tag "Search", :name => nil %>
</p>

这是我的 rSpec 代码。这是我在尝试实现 Thinking Sphinx 之前使用的原始代码。

describe "index" do
  let(:user) { FactoryGirl.create(:user) }
  before(:each) do
    sign_in user
    visit users_path
  end
  it { should have_selector('title', text: 'All users') }

  describe "pagination" do
    before(:all) { 15.times { FactoryGirl.create(:user) } }
    after(:all)  { User.delete_all }
    let(:first_page)  { User.paginate(page: 1) }
    let(:second_page) { User.paginate(page: 2) }
    it { should have_link('Next') }
    it { should have_link('2') }
    it { should_not have_link('delete') }
    it "should list each user" do
      User.all[0..2].each do |user|
        page.should have_selector('li', text: user.name)
      end
    end

    it "should list the first page of users" do
      first_page.each do |user|
        page.should have_selector('li', text: user.name)
      end
    end
    it "should not list the second page of users" do
      second_page.each do |user|
        page.should_not have_selector('li', text: user.name)
      end
    end
    describe "as an admin user" do
      let(:admin) { FactoryGirl.create(:admin) }
      before do
        sign_in admin
        visit users_path
      end
      it { should have_link('delete', href: user_path(User.first)) }
      it "should be able to delete another user" do
        expect { click_link('delete') }.to change(User, :count).by(-1)
      end
      it { should_not have_link('delete', href: user_path(admin)) }
    end

  end
end

当我运行 rSpec 测试时,我收到以下错误:

Failure/Error: visit users_path
     ActionView::Template::Error:
       getaddrinfo: nodename nor servname provided, or not known

页面显示得很好。当我在搜索框中输入文本并单击按钮时,没有任何反应,这对我来说并不奇怪。

我可以在终端上运行 Sphinx。搜索工作正常。我只是不知道如何用 Thinking Sphinx 调试这个问题。在过去的几天里,我搜索了这个网站上的许多页面和许多其他页面,但没有一个页面处理这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails-3 rspec2 thinking-sphinx


    【解决方案1】:

    你初始化 sphinx 数据库了吗?

    通常您只需要执行以下操作:

    rake ts:rebuild
    

    这应该会自动为您运行 rake ts:conf 并重建您的索引。

    您也可以使用rake ts:in 来更新索引。

    测试这是否有效的一种简单方法是运行 rails 控制台 (rails c) 并尝试手动搜索您的用户 (User.search)。

    如果您得到任何结果,则索引可用,接下来您可以对视图/控制器进行故障排除;)

    【讨论】:

    • Maximus,因为生活,我不得不放弃这个工作:) 我只是按照你的建议做了。认为 Sphinx 发现记录很好。我需要弄清楚如何更新视图/控制器。感谢您的帮助。
    • 因为我在 Rails 应用程序中使用 PostgreSQL,所以我决定为我的文本搜索安装 pg_search gem。一切都在引擎盖下。它非常适合文本搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多