I'm integrating a simple indexed search for a rails 5.1.4
application, using Sphinx 2.2.11-id64-release (95ae9a6)
and thinking_sphinx v 4.0.0
Expected behavior:
When I submit a new search, I expect to see either an empty array []
or a set of search results.
Actual behavior:
When I submit a new search with empty parameters from the view layer and try to access the ThinkingSphinx::Search object via a binding.pry
in the controller action, rails throws a 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)>
Code snippets:
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>
Here's the 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
}
I think the issue here is not directly related to Thinking Sphinx - it just errors because it can't load the search results due to the circular dependency in your models - particularly, the LESSON_TYPES
constant:
StudentLesson
instance, so it needs to load that model. StudentLesson
finds its dependency (as a subclass) on Lesson
. Lesson
finds its dependency (as references to constants) on both StudentLesson
and ProfessionalLesson
. StudentLesson
is attempted to be loaded again, and hence the endless loop of dependencies. (FWIW I just confirmed this behaviour in a test Rails app using the model code you've provided, without TS being involved: all I needed to run in a console was StudentLesson.first
.)
You have 2 classes which both inherit a constant definition, this looks problematic.
Try moving this constant definition to an initializer:
LESSON_TYPES = {
'StudentLesson': StudentLesson,
'ProfessionalLesson': ProfessionalLesson
}.freeze
解决方案实际上是在注释线程中找到的spring仍然未解决的问题,可通过初始化require_dependency 'lesson'
->来解决,该问题我实际上已经在初始化程序中有了,但将其移至已解决的Rails.application.config.to_prepare
块中重新加载问题以及与狮身人面像相关的症状。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.