【问题标题】:Solr 404 error with sunspot on travis-citravis-ci 上的太阳黑子出现 Solr 404 错误
【发布时间】:2026-02-16 10:25:01
【问题描述】:

我正在考虑为我的 Ruby on Rails 应用程序从自定义 Jenkins 迁移到 Travis。

我的应用程序使用 Sunspot 运行 Solr 服务器,并在真实 Solr 实例上使用 rspec 测试查询。 我正在使用 sunspot_solr gem 女巫,它具有预打包的 Solr 发行版,还有 sunspot-rails-tester gem 仅在需要时启动 Sunspot。

它在我们的本地机器和我们的 Jenkins 服务器上运行良好。

但是我现在和 Travis 有问题……

根据我在 Travis 上看到的日志,Sunspot 似乎启动了:

Sunspot server is starting...
Sunspot server is starting...
Sunspot server is starting...
Sunspot server took 4.10 seconds to start

但是当 rspec 测试尝试进行查询时,我从 Solr 收到 404 错误:

 RSolr::Error::Http:
   RSolr::Error::Http - 404 Not Found
   Error:     Not Found

   Request Data: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>*:*</query></delete>"

我有一个标准的 config/sunspot.yml 配置:

development:
  solr:
    hostname: localhost
    port: 8982
    log_level: INFO
    path: /solr/development

test:
  solr:
    hostname: localhost
    port: 8981
    log_level: WARNING
    path: /solr/test

这是我的 .travis.yml 配置:

language: ruby
rvm:
  - "2.1.3"
cache: bundler
env:
  - DB=postgresql
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake
before_script:
  - "psql -c 'create database test;' -U postgres"
  - cp config/database.travis.yml config/database.yml
  - cp config/sunspot.travis.yml config/sunspot.yml

编辑

我在这里找到了一个解决方法:Rails app: Solr throwing RSolr::Error::Http - 404 Not Found when executing search

我的 sunspot.yml 现在是:

development:
  solr:
    hostname: localhost
    port: 8982
    log_level: INFO
    path: /solr/development

test:
  solr:
    hostname: localhost
    port: 8981
    log_level: WARNING
    path: /solr/test
    solr_home: solr

我不知道这里到底出了什么问题,以及为什么我必须只为 Travis 这样做......

但是,当我设置“solr_home: solr”时,solr 会安装在这里:

/home/travis/build/myapp/web/vendor/bundle/ruby/2.1.0/gems/sunspot_solr-2.1.1/solr/solr

所以我必须将我的 solr/conf/schema.xml 复制到这个文件夹中,这不太可能,我最终得到了这个 travis.yml

language: ruby
rvm:
  - "2.1.3"
cache: bundler
env:
  - DB=postgresql
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rake db:test:prepare
  - bundle exec rake
before_script:
  - "psql -c 'create database \"elcurator-test\";' -U postgres"
  - cp config/database.travis.yml config/database.yml
  - cp config/sunspot.travis.yml config/sunspot.yml
  - mkdir -p /home/travis/build/myapp/web/vendor/bundle/ruby/2.1.0/gems/sunspot_solr-2.1.1/solr/solr/conf
  - cp solr/conf/schema.xml /home/travis/build/myapp/web/vendor/bundle/ruby/2.1.0/gems/sunspot_solr-2.1.1/solr/solr/conf

所以主要问题仍然存在,为什么我在使用默认的 solr_home(即“#{Rails.root}/solr”)时会出现 404 错误?

【问题讨论】:

    标签: ruby-on-rails solr rspec sunspot travis-ci


    【解决方案1】:

    另一个关于 SO 的帖子暗示您在 sunspot.yml 中指定的端口可能不是最终使用的端口。

    尝试使用端口 8983,或在 /solr/conf/scripts.conf 中指定的默认端口

    参考:Connection refused using Sunspot and Solr in Rails

    【讨论】:

    • 感谢@Keysharpener,我尝试使用端口 8983,但同样的问题......实际上太阳黑子宝石应该使用 sunspot.yml 来选择你使用的巫婆端口。
    • 这里我觉得有些奇怪。在 before_script 任务中运行cp config/sunspot.travis.yml config/sunspot.yml 时不是覆盖 sunspot.yml 吗? sunspot.travis.yml 中的端口是什么?
    • 在问题中检查我的编辑。 Solr 现在运行良好,但现在我的 solr conf 被忽略了。我不知道 solr_home 选项的作用。
    【解决方案2】:

    检查您是否正确地对文件夹 /solr/test 进行了版本控制

    它应该有文件solr/test/core.properties 和一个文件夹solr/test/data

    缺少太阳黑子 404 通常是缺少这些文件。

    test 环境如此,其他环境也如此。

    【讨论】: