【问题标题】:Using sunspot gem and solr for indexing search results使用 sunspot gem 和 solr 索引搜索结果
【发布时间】:2014-02-25 12:01:14
【问题描述】:

使用 rails 3.0.6 ,ruby 1.9.3

Gemfile(根据需要粘贴)

gem 'rails', '3.0.6',

gem '耙子','0.9.2'

gem 'sunspot_rails'

宝石'sunspot_solr'

gem 'progress_bar'

我已经在 user.rb 中设置了这样的模型

Sunspot.setup(User) do
  text :resume_body, :stored => true
  text :job_title, :stored => true
 end

然后我用过

class CandidatesController < ApplicationController

  def index
    @search = Sunspot.search(User) do
      # Full text on resume body and job title with highlighting
      keywords params[:q] do
        highlight :resume_body, :max_snippets => 3, :fragment_size => 120, :merge_contiguous_fragments => true
      end

每当我运行 rake 任务时,例如 bundle exec rake 太阳黑子:solr:reindex 耙太阳黑子:重新索引

报错

错误

    bundle exec rake sunspot:solr:reindex --trace
Your Gemfile lists the gem rspec-rails (>= 2.0.0.beta.19) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Your Gemfile lists the gem mysql2 (~> 0.2.22) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Your Gemfile lists the gem mysql2 (~> 0.2.22) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
** Invoke sunspot:solr:reindex (first_time)
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
** Execute sunspot:reindex
rake aborted!
undefined method `solr_reindex' for #<Class:0xb762a7c>
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.0.6/lib/active_record/base.rb:1008:in `method_missing'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot_rails-2.1.0/lib/sunspot/rails/tasks.rb:57:in `block (4 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot-2.1.0/lib/sunspot/class_set.rb:16:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot-2.1.0/lib/sunspot/class_set.rb:16:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot_rails-2.1.0/lib/sunspot/rails/tasks.rb:56:in `block (3 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot_rails-2.1.0/lib/sunspot/rails/tasks.rb:68:in `with_session'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot_rails-2.1.0/lib/sunspot/rails/tasks.rb:19:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:174:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p448/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p448/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => sunspot:solr:reindex => sunspot:reindex

注意 - 该应用之前使用 sunspot 1.1,我已将其升级到 2.1

这是我第一次在开发中尝试 solr 和 sunspot,我也遵循了所有教程以确保安装正确。但是,我可能还是错过了一些东西。非常感谢任何帮助将新手指向正确方向的帮助。在此先感谢,我也非常打算在生产中使用 sunspot :)

更新 1 -

tail -f log/sunspot-solr-development.log
    at org.apache.solr.core.Config.<init>(Config.java:103)
    at org.apache.solr.core.Config.<init>(Config.java:73)
    at org.apache.solr.core.SolrConfig.<init>(SolrConfig.java:117)
    at org.apache.solr.core.CoreContainer.createFromLocal(CoreContainer.java:989)
    ... 9 more

Feb 25, 2014 1:31:54 PM org.apache.solr.servlet.SolrDispatchFilter init
INFO: user.dir=/usr/local/rvm/gems/ruby-1.9.3-p448/gems/sunspot_solr-2.1.0/solr
Feb 25, 2014 1:31:54 PM org.apache.solr.servlet.SolrDispatchFilter init
INFO: SolrDispatchFilter.init() done

更新 2 -

Loading development environment (Rails 3.0.6)
1.9.3-p448 :001 > User.reindex
NoMethodError: undefined method `reindex' for #<Class:0x9910fd8>
    from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.0.6/lib/active_record/base.rb:1008:in `method_missing'
    from (irb):1
    from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

【问题讨论】:

  • 当太阳黑子给我废话时我总是检查的事情清单:我rake sunspot:solr:start。我bundle install。我rails generate sunspot_rails install.此外,您是否有理由在用户模型上使用 Sunspot.setup 而不是 searchable 块?这不是造成这种情况的原因,我只是好奇。
  • 另外,请确保您对此进行了很好的测试。 Sunspot 2.1.0 将打包的 SOLR 版本从 3.5 更改为 4.2...
  • 我确实尝试了可搜索块,因为这是一个曾经使用太阳黑子 1.1 但没有运气的旧应用程序,你提到的命令也成功触发,我也得到了结果,但搜索不到索引。
  • 我正在使用带有太阳黑子 2.1 的 solr 4.6。
  • 还想问,新的黑子宝石只索引文本搜索?特定用户的经纬度搜索呢?

标签: ruby-on-rails ruby ruby-on-rails-3 solr sunspot


【解决方案1】:

这可能会帮助你搭车.....

太阳黑子快速入门的步骤:

  1. 将此 gem 添加到您的 gemfile 中

    gem 'sunspot_rails'
    
  2. 安装这个 gem 使用

    bundle install
    
  3. 使用此命令启动 solr

    rake sunspot:solr:start
    
  4. 举个简单的例子,考虑一个带有标题字段和内容字段的 Post 模型。我们会将这些字段索引为 Solr 文本字段。

    rake sunspot:solr:reindex
    
  5. 这是对我们上面索引的 Post 类的简单搜索,从 PostsController 的 index 操作运行:

    class PostsController < ApplicationController
     def index
       @search = Post.search do
         keywords params[:query]
       end
       @posts = @search.results
     end
    end
    
  6. 现在我们可以在视图中使用@posts 变量来执行index 操作。结果会随之而来

注意:这只是理解 flow 和 get 的一个最简单的例子 从太阳黑子 solr 索引和搜索开始......有很多 其他应根据我们的要求使用的选项。

【讨论】:

  • 您能总结一下您认为链接对您有什么帮助吗?链接有随时间消失的习惯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多