我正在使用Sphinx 2.2.11-id64-release (95ae9a6)
和thinking_sphinx v 4.0.0
集成对rails 5.1.4
应用程序的简单索引搜索
预期行为:
当我提交新搜索时,我希望看到一个空数组[]
或一组搜索结果。
实际行为:
当我从视图层提交一个包含空参数的新搜索并尝试通过控制器操作中的binding.pry
访问 ThinkingSphinx::Search 对象时,rails 会抛出ActionView::Template::Error (Circular dependency detected while autoloading constant StudentLesson)
[1] pry(main)> ThinkingSphinx.search ''
=> [#<ThinkingSphinx::Search:0x2b0925399e10>
[2] pry(main)> _.inspect
RuntimeError: Circular dependency detected while autoloading constant StudentLesson
from /home/kf/.rvm/gems/ruby-2.4.3@crm/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:509:in `load_missing_constant'
[3] pry(main)>
代码片段:
class Lesson < ApplicationRecord
LESSON_TYPES = {
'StudentLesson': StudentLesson,
'ProfessionalLesson': ProfessionalLesson
}.freeze
end
class StudentLesson < Lesson
after_save ThinkingSphinx::RealTime.callback_for(:student_lesson)
end
class ProfessionalLesson < Lesson
after_save ThinkingSphinx::RealTime.callback_for(:professional_lesson)
end
# app/indices/student_lesson_index.rb
ThinkingSphinx::Index.define :student_lesson, with: :real_time do
indexes name, sortable: true
end
# app/indices/professional_lesson_index.rb
ThinkingSphinx::Index.define :professional_lesson, with: :real_time do
indexes name, sortable: true
end
class SearchesController < ApplicationController
def index
@results = []
end
def create
@results = ThinkingSphinx.search(params[:search])
render :index
end
end
<div class="collapse navbar-collapse" id="header-navbar">
<%= render 'layouts/nav_links' %>
<%= form_for searches_path do %>
<%= search_field_tag :search, params[:search] %>
<%= submit_tag 'Search', name: nil, method: :get %>
<% end %>
</div>
这是dev.sphinx.conf
indexer
{
}
searchd
{
listen = 127.0.0.1:9306:mysql41
log = /home/myapp/log/development.searchd.log
query_log = /home/myapp/log/development.searchd.query.log
pid_file = /home/myapp/log/development.sphinx.pid
workers = threads
binlog_path = /home/myapp/tmp/binlog/development
}
index game_core
{
type = rt
path = /home/myapp/db/sphinx/development/game_core
docinfo = extern
rt_field = sphinx_internal_class_name
rt_field = name
rt_field = summary
rt_attr_uint = sphinx_deleted
rt_attr_bigint = sphinx_internal_id
rt_attr_timestamp = created_at
rt_attr_timestamp = updated_at
rt_attr_string = sphinx_internal_class
rt_attr_string = name_sort
}
index lesson_core
{
type = rt
path = /home/myapp/db/sphinx/development/lesson_core
docinfo = extern
rt_field = sphinx_internal_class_name
rt_field = name
rt_field = purpose
rt_field = meta
rt_field = supplies
rt_field = activity
rt_attr_uint = sphinx_deleted
rt_attr_bigint = sphinx_internal_id
rt_attr_timestamp = created_at
rt_attr_timestamp = updated_at
rt_attr_string = sphinx_internal_class
rt_attr_string = name_sort
}
index protocol_core
{
type = rt
path = /home/myapp/db/sphinx/development/protocol_core
docinfo = extern
rt_field = sphinx_internal_class_name
rt_field = name
rt_field = description
rt_attr_uint = sphinx_deleted
rt_attr_bigint = sphinx_internal_id
rt_attr_timestamp = created_at
rt_attr_timestamp = updated_at
rt_attr_string = sphinx_internal_class
rt_attr_string = name_sort
}
index resource_page_core
{
type = rt
path = /home/myapp/db/sphinx/development/resource_page_core
docinfo = extern
rt_field = sphinx_internal_class_name
rt_field = header
rt_field = content
rt_attr_uint = sphinx_deleted
rt_attr_bigint = sphinx_internal_id
rt_attr_timestamp = created_at
rt_attr_timestamp = updated_at
rt_attr_string = sphinx_internal_class
rt_attr_string = header_sort
}
index game
{
type = distributed
local = game_core
}
index lesson
{
type = distributed
local = lesson_core
}
index protocol
{
type = distributed
local = protocol_core
}
index resource_page
{
type = distributed
local = resource_page_core
}
我认为这里的问题与思考狮身人面像没有直接关系 - 它只是错误,因为它无法加载搜索结果由于模型中的循环依赖性 - 特别是LESSON_TYPES
常量:
- 思考狮身人面像进行搜索调用,在其结果集中,它至少有一个
StudentLesson
实例,因此它需要加载该模型。 - 加载
StudentLesson
在Lesson
上找到其依赖项(作为子类(。 - 加载
Lesson
在StudentLesson
和ProfessionalLesson
上找到其依赖关系(作为对常量的引用(。 - 因此,尝试再次加载
StudentLesson
,因此依赖项的无限循环。
(FWIW 我刚刚使用您提供的模型代码在测试 Rails 应用程序中确认了这种行为,没有涉及 TS:我只需要在控制台中运行StudentLesson.first
。
你有 2 个类,它们都继承了一个常量定义,这看起来有问题。
尝试将此常量定义移动到初始值设定项:
LESSON_TYPES = {
'StudentLesson': StudentLesson,
'ProfessionalLesson': ProfessionalLesson
}.freeze
解决方案实际上是在评论线程中找到的,用于解决 spring 仍然未解决的问题,通过初始化 require_dependency 'lesson'
-> 来解决,我实际上已经在初始值设定项中拥有它,但将其移动到 Rails.application.config.to_prepare
块解决了重新加载问题,其中与狮身人面像相关的症状。